Mor*_*oth 7 google-sheets google-spreadsheet-api
我想引用合并的单元格并从属于合并单元格范围内的任何单元格获取合并单元格的值。
=CONCATENATE(A2, " ",B2)
在 C1 中使用并将其向下拖动适用于:未合并的单元格,以及合并单元格的第一个单元格。它不适用于合并组中的后续单元格。这在 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
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,如果一个合并列位于另一合并列的正上方,并且底部合并列为空,则脚本将使用顶部合并列的值。
下面使用它,D列显示B列中的公式,成功或错误情况显示在C列中。
归档时间: |
|
查看次数: |
9169 次 |
最近记录: |