gpt4 book ai didi

google-sheets - 对合并单元格的 Google 电子表格单元格引用

转载 作者:行者123 更新时间:2023-12-03 16:26:00 24 4
gpt4 key购买 nike

我想引用合并的单元格并从属于合并单元格范围内的任何单元格获取合并单元格的值。

使用 =CONCATENATE(A2, " ",B2)在 C1 中并将其向下拖动适用于:未合并的单元格,以及合并单元格的第一个单元格。它不适用于合并组中的后续单元格。这在 MS Excel 中按预期工作,但在 Google 电子表格中无效,有没有办法实现这一点?

Spreadsheet example

最佳答案

我编写了以下脚本,该脚本仅适用于合并列(这是我想要的):

/**
* 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列中。

enter image description here

关于google-sheets - 对合并单元格的 Google 电子表格单元格引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41967357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com