Google 对合并单元格的电子表格单元格引用
Google Spreadsheet cell reference to merged cell
我想引用合并单元格并从合并单元格范围内的任何单元格中获取合并单元格的值。
在 C1 中使用 =CONCATENATE(A2, " ",B2)
并将其向下拖动适用于:未合并的单元格和合并单元格的第一个单元格。它不适用于合并组中的后续单元格。这在 MS Excel 中按预期工作,但在 Google 电子表格中没有,有没有办法实现这个?
我编写了以下脚本,它仅适用于合并列(这正是我想要的):
/**
* Returns the value at the top of a merged cell. If the merged cell is blank, not at the top of the sheet, and below another merged cell, the the function overflows into the above merged cell and returns incorrect input.
*
* @param {int} column Column number of the cell.
* @param {int} row Row number of the cell.
* @return The value shown in the merged cell.
* @customfunction
*/
function FIRST_IN_MERGED_COLUMN(column, row) {
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getRange(row, column);
if (!cell.isPartOfMerge())
return cell.getValue();
else if (cell.getValue() != "")
return cell.getValue();
else {
var r = row;
var newCell = cell, oldCell = cell;
while (r > 1) {
if (!(newCell = sheet.getRange(--r, column)).isPartOfMerge())
return oldCell.getValue();
if (newCell.getValue() != "")
return newCell.getValue();
oldCell = newCell;
}
return ""; // Blank merged cell at top of sheet
}
}
遗憾的是,如果一个合并列直接位于另一个合并列之上,并且底部合并列为空白,则脚本将使用顶部合并列的值。
下面用到,D列B列公式,C列成功或错误情况
我找到了更好的方法:
// example inputs for cell position
var row = 5;
var column = 7;
// Get the range that the cell belongs and then get the displayed value.
var cell = sheet.getRange(row, column);
var result;
if (!cell.isPartOfMerge()) {
result = cell.getValue();
} else {
// get the range the cell belongs
var range = cell.getMergedRanges()[0];
// getting the column and the row of the top left cell in the range
var colRef = range.getColumn();
var rowRef = range.getRow();
// getting the value
result = range.getDisplayValue();
}
我结合了这个函数的其他答案:
function MERGED_CELL_VALUE(column, row) {
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getRange(row, column);
if (!cell.isPartOfMerge())
return cell.getValue();
else if (cell.getValue() != "")
return cell.getValue();
else {
var range = cell.getMergedRanges()[0];
return range.getDisplayValue();
}
}
这是我试图整理所有其他答案的所有最佳属性的尝试。我已经用我能想到的每一种方式对它进行了酷刑测试,它似乎经受住了考验。也就是说,欢迎错误报告。
用法:用引号中的单元格引用调用它:=MERGED_CELL_VALUE("H19")
,而不是 =MERGED_CELL_VALUE(H19)
。如果您在调用函数时不带引号,我无法使该函数始终引发信息性错误,因为 H19 的 contents 很容易成为 A2
或其他内容。但它通常会以有用的方式抱怨。
如果您希望能够使用点击拉动自动填充功能,有一个相当简单的解决方法。像这样使用 CELL()
:=MERGED_CELL_VALUE(CELL("address", H19))
。现在,H19 是空的,所以如果你移动它就会更新。这个技巧也适用于 H
等。
/**
* Returns the value at the top of a merged cell.
*
* Inspired by the code at:
*
*
* @param {str} target The targeted cell, as a string in A1 format.
* @return The value shown in the merged cell.
* @customfunction
*/
function MERGED_CELL_VALUE(target) {
var sheet = SpreadsheetApp.getActiveSheet();
try {
var targetRange = sheet.getRange(target);
} catch(e) {
if (target == "") {
throw "Empty target specified. Did you forget to enclose the targeted cell in quotes?"
}
else {
throw "Invalid target '" + target + "'. Did you forget to enclose the cell ID in quotes?";
}
}
if (!(targetRange.getNumColumns() == targetRange.getNumRows() == 1)) {
throw "This function does not work on ranges (e.g. 'A1:B3'), only individual cells.";
}
var cell = targetRange.getCell(1, 1); // n.b. Range.getCell() is 1-indexed because FML.
if (!cell.isPartOfMerge()) {
// If the cell is not part of a merge, return the cell's value.
return cell.getValue();
}
else if (cell.getValue() != "") { // n.b. this is a special case for getValue(); empty returns "".
// If the cell has a value, even if it's merged (!!??), return the value.
return cell.getValue();
}
else {
/*
* This might turn out to be a bug; we pick getMergedRanges()[0]. Hopefully that matches how GSheets's logic works.
* There is a getDisplayValue(), but that always returns a String, which is unlikely to fit our goal in
* all cases.
*/
return cell.getMergedRanges()[0].getCell(1, 1).getValue();
}
}
我想引用合并单元格并从合并单元格范围内的任何单元格中获取合并单元格的值。
在 C1 中使用 =CONCATENATE(A2, " ",B2)
并将其向下拖动适用于:未合并的单元格和合并单元格的第一个单元格。它不适用于合并组中的后续单元格。这在 MS Excel 中按预期工作,但在 Google 电子表格中没有,有没有办法实现这个?
我编写了以下脚本,它仅适用于合并列(这正是我想要的):
/**
* Returns the value at the top of a merged cell. If the merged cell is blank, not at the top of the sheet, and below another merged cell, the the function overflows into the above merged cell and returns incorrect input.
*
* @param {int} column Column number of the cell.
* @param {int} row Row number of the cell.
* @return The value shown in the merged cell.
* @customfunction
*/
function FIRST_IN_MERGED_COLUMN(column, row) {
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getRange(row, column);
if (!cell.isPartOfMerge())
return cell.getValue();
else if (cell.getValue() != "")
return cell.getValue();
else {
var r = row;
var newCell = cell, oldCell = cell;
while (r > 1) {
if (!(newCell = sheet.getRange(--r, column)).isPartOfMerge())
return oldCell.getValue();
if (newCell.getValue() != "")
return newCell.getValue();
oldCell = newCell;
}
return ""; // Blank merged cell at top of sheet
}
}
遗憾的是,如果一个合并列直接位于另一个合并列之上,并且底部合并列为空白,则脚本将使用顶部合并列的值。
下面用到,D列B列公式,C列成功或错误情况
我找到了更好的方法:
// example inputs for cell position
var row = 5;
var column = 7;
// Get the range that the cell belongs and then get the displayed value.
var cell = sheet.getRange(row, column);
var result;
if (!cell.isPartOfMerge()) {
result = cell.getValue();
} else {
// get the range the cell belongs
var range = cell.getMergedRanges()[0];
// getting the column and the row of the top left cell in the range
var colRef = range.getColumn();
var rowRef = range.getRow();
// getting the value
result = range.getDisplayValue();
}
我结合了这个函数的其他答案:
function MERGED_CELL_VALUE(column, row) {
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getRange(row, column);
if (!cell.isPartOfMerge())
return cell.getValue();
else if (cell.getValue() != "")
return cell.getValue();
else {
var range = cell.getMergedRanges()[0];
return range.getDisplayValue();
}
}
这是我试图整理所有其他答案的所有最佳属性的尝试。我已经用我能想到的每一种方式对它进行了酷刑测试,它似乎经受住了考验。也就是说,欢迎错误报告。
用法:用引号中的单元格引用调用它:=MERGED_CELL_VALUE("H19")
,而不是 =MERGED_CELL_VALUE(H19)
。如果您在调用函数时不带引号,我无法使该函数始终引发信息性错误,因为 H19 的 contents 很容易成为 A2
或其他内容。但它通常会以有用的方式抱怨。
如果您希望能够使用点击拉动自动填充功能,有一个相当简单的解决方法。像这样使用 CELL()
:=MERGED_CELL_VALUE(CELL("address", H19))
。现在,H19 是空的,所以如果你移动它就会更新。这个技巧也适用于 H
等。
/**
* Returns the value at the top of a merged cell.
*
* Inspired by the code at:
*
*
* @param {str} target The targeted cell, as a string in A1 format.
* @return The value shown in the merged cell.
* @customfunction
*/
function MERGED_CELL_VALUE(target) {
var sheet = SpreadsheetApp.getActiveSheet();
try {
var targetRange = sheet.getRange(target);
} catch(e) {
if (target == "") {
throw "Empty target specified. Did you forget to enclose the targeted cell in quotes?"
}
else {
throw "Invalid target '" + target + "'. Did you forget to enclose the cell ID in quotes?";
}
}
if (!(targetRange.getNumColumns() == targetRange.getNumRows() == 1)) {
throw "This function does not work on ranges (e.g. 'A1:B3'), only individual cells.";
}
var cell = targetRange.getCell(1, 1); // n.b. Range.getCell() is 1-indexed because FML.
if (!cell.isPartOfMerge()) {
// If the cell is not part of a merge, return the cell's value.
return cell.getValue();
}
else if (cell.getValue() != "") { // n.b. this is a special case for getValue(); empty returns "".
// If the cell has a value, even if it's merged (!!??), return the value.
return cell.getValue();
}
else {
/*
* This might turn out to be a bug; we pick getMergedRanges()[0]. Hopefully that matches how GSheets's logic works.
* There is a getDisplayValue(), but that always returns a String, which is unlikely to fit our goal in
* all cases.
*/
return cell.getMergedRanges()[0].getCell(1, 1).getValue();
}
}