【问题标题】:Google sheets API fetch the value of Merged cellsGoogle Sheets API 获取合并单元格的值
【发布时间】:2023-03-12 13:16:01
【问题描述】:

我正在使用 Google Sheets API 在我的 javascript 应用程序中显示来自我的 Google 电子表格的数据。通过引用此documentation 进行开发。我可以从电子表格中读取值,但对于某些列,单元格合并如下。

我得到的 Json 响应为

[
[
  "Name",
  "Age"
 ],
[
  "John",
  "25"
 ],
[
  "Doe"
 
 ],
]

我的预期输出应该是

   [
[
  "Name",
  "Age"
 ],
[
  "John",
  "25"
 ],
[
  "Doe",
   "25"
 
 ],
]

有什么帮助吗?

【问题讨论】:

    标签: javascript google-sheets google-sheets-api google-developers-console


    【解决方案1】:

    要检索值,您必须测试单元格是否是合并区域的一部分。

    var value = (cell.isPartOfMerge() ? cell.getMergedRanges()[0].getCell(1,1) : cell).getValue();
    

    【讨论】:

    • 嗨,在代码中哪里使用这个?请帮助 js 文档示例。 developers.google.com/sheets/api/quickstart/js
    • 分享您的 javascript 应用程序(简化版),这样会更容易...
    • 它与文档中给出的 index.html 文件完全相同。只有更改是我的 api 凭据。
    • 我认为您也将 row[4] 更改为 row[2],但是,请尝试我的其他答案或发布您自己的脚本
    【解决方案2】:

    修改脚本如下

        appendPre('Name, Major:');
        var lastData=''
        for (i = 0; i < range.values.length; i++) {
          var row = range.values[i];
          if (row[4]!=''){lastData=row[4]}
          // Print columns A and lastData, which correspond to indices 0 and the last known value in column E that contains merged cells.
          appendPre(row[0] + ', ' + lastData);
        }
    

    以便在单元格为空时调用最后一个值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      相关资源
      最近更新 更多