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

标签 google-sheets google-spreadsheet-api

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

使用 =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/

相关文章:

javascript - 我可以将用户帐户与驱动器 API 的操作关联起来吗?

java - 如何使用服务帐户创建 Google 电子表格并共享给 java 中的其他 google 用户?

api - 新的 Google 电子表格导致 Google Visualization API 查询失败

google-apps-script - 仅当符合条件时才查找编号最大的表条目

java - 通过 Java 访问 Sheet API 时的 NoSuchMethod

python - 使用python将google docs公共(public)电子表格下载到csv

android - 在 Android 中使用结构查询 (sq) 从 Google Spreadsheet API 检索数据时出错

google-apps-script - YouTubeAnalytics.Reports.query:对关联 channel 的身份验证

c# - 使用 C# 访问私有(private) Google 电子表格

google-apps-script - Apps Script 中的 getActive() 和 getActiveSpreadsheet() 有什么区别?