使用 Google Apps 脚本填充 Google 文档中的表格单元格

新人

我想将一个 5x5 的表格添加到一个空文档中。最终,我想在几页中的每一页上添加一个。文档很神秘,但我从 Tanaike 找到了这个很好的例子:

它有效https://tanaikech.github.io/2019/05/22/creating-new-table-and-putting-values-to-cells-using-google-docs-api-with-google-apps-script/

  var resource = {
    requests: [
      { insertTable: { rows: 2, columns: 2, location: { index: 1 } } },
      { insertText: { text: "B2", location: { index: 12 } } },
      { insertText: { text: "A2", location: { index: 10 } } },
      { insertText: { text: "B1", location: { index: 7 } } },
      { insertText: { text: "A1", location: { index: 5 } } }
    ]
  };
  Docs.Documents.batchUpdate(resource, doc.getId());

因此,我尝试扩展到具有相同模式的 5 x 5 表并收到以下错误:Error GoogleJsonResponseException: API call to docs.documents.batchUpdate failed with error: Invalid requests[5] .insertText: The inserting index must be inside现有段落的边界。您仍然可以通过插入换行符来创建新段落。

  var requests = []
  requests.push ({ insertTable: 
                   { rows: 5, columns: 5, 
                     location: { index: 1 } }  
                 });
  requests.push ( { insertText: { text: "E5", location: { index: 33 } } } );  // 2
  requests.push ( { insertText: { text: "D5", location: { index: 31 } } } );  // 3
  requests.push ( { insertText: { text: "C5", location: { index: 29 } } } );  // 4
  requests.push ( { insertText: { text: "B5", location: { index: 27 } } } );  // 5
  requests.push ( { insertText: { text: "A5", location: { index: 25 } } } );  // 6
  requests.push ( { insertText: { text: "E4", location: { index: 28 } } } );  // 7
  requests.push ( { insertText: { text: "D4", location: { index: 26 } } } );  // 8
  requests.push ( { insertText: { text: "C4", location: { index: 24 } } } );
  requests.push ( { insertText: { text: "B4", location: { index: 22 } } } );
  requests.push ( { insertText: { text: "A4", location: { index: 20 } } } );
  requests.push ( { insertText: { text: "E3", location: { index: 23 } } } );
  requests.push ( { insertText: { text: "D3", location: { index: 21 } } } );
  requests.push ( { insertText: { text: "C3", location: { index: 19 } } } );
  requests.push ( { insertText: { text: "B3", location: { index: 17 } } } );
  requests.push ( { insertText: { text: "A3", location: { index: 15 } } } );
  requests.push ( { insertText: { text: "E2", location: { index: 18 } } } );
  requests.push ( { insertText: { text: "D2", location: { index: 16 } } } );
  requests.push ( { insertText: { text: "C2", location: { index: 14 } } } );
  requests.push ( { insertText: { text: "B2", location: { index: 12 } } } );
  requests.push ( { insertText: { text: "A2", location: { index: 10 } } } );
  requests.push ( { insertText: { text: "E1", location: { index: 13 } } } );
  requests.push ( { insertText: { text: "D1", location: { index: 11 } } } );
  requests.push ( { insertText: { text: "C1", location: { index: 09 } } } );
  requests.push ( { insertText: { text: "B1", location: { index: 07 } } } );
  requests.push ( { insertText: { text: "A1", location: { index: 05 } } } );

  if (requests.length > 0)   {
    response = Docs.Documents.batchUpdate(
      { 'requests': requests }, doc.getId());
  }

appendParagraph(..) 和 appendTable() 都是文档正文的有效方法,所以我认为我不需要在段落内添加带有表格的段落?

更多研究和Tanaike也在这里回答

无法通过 Docs API 将文本添加到新的 Google Doc我添加了 endOfSegmentLocation 并得到以下 GoogleJsonResponseException: API call to docs.documents.batchUpdate failed with error: Invalid JSON payload received。“请求 [0]”中的未知名称“endOfSegmentLocation”:找不到字段。

  var requests = []
  requests.push ({ insertTable: 
                   { rows: 5, columns: 5, 
                     location: { index: 1 } },
                     endOfSegmentLocation: { segmentId: "" }   
                 });
  requests.push ( { insertText: { text: "E5", location: { index: 33 } } } );  // 2
  requests.push ( { insertText: { text: "D5", location: { index: 31 } } } );  // 3
  requests.push ( { insertText: { text: "C5", location: { index: 29 } } } );  // 4
  requests.push ( { insertText: { text: "B5", location: { index: 27 } } } );  // 5
  requests.push ( { insertText: { text: "A5", location: { index: 25 } } } );  // 6
  requests.push ( { insertText: { text: "E4", location: { index: 28 } } } );  // 7
  requests.push ( { insertText: { text: "D4", location: { index: 26 } } } );  // 8
  requests.push ( { insertText: { text: "C4", location: { index: 24 } } } );
  requests.push ( { insertText: { text: "B4", location: { index: 22 } } } );
  requests.push ( { insertText: { text: "A4", location: { index: 20 } } } );
  requests.push ( { insertText: { text: "E3", location: { index: 23 } } } );
  requests.push ( { insertText: { text: "D3", location: { index: 21 } } } );
  requests.push ( { insertText: { text: "C3", location: { index: 19 } } } );
  requests.push ( { insertText: { text: "B3", location: { index: 17 } } } );
  requests.push ( { insertText: { text: "A3", location: { index: 15 } } } );
  requests.push ( { insertText: { text: "E2", location: { index: 18 } } } );
  requests.push ( { insertText: { text: "D2", location: { index: 16 } } } );
  requests.push ( { insertText: { text: "C2", location: { index: 14 } } } );
  requests.push ( { insertText: { text: "B2", location: { index: 12 } } } );
  requests.push ( { insertText: { text: "A2", location: { index: 10 } } } );
  requests.push ( { insertText: { text: "E1", location: { index: 13 } } } );
  requests.push ( { insertText: { text: "D1", location: { index: 11 } } } );
  requests.push ( { insertText: { text: "C1", location: { index: 09 } } } );
  requests.push ( { insertText: { text: "B1", location: { index: 07 } } } );
  requests.push ( { insertText: { text: "A1", location: { index: 05 } } } );

  if (requests.length > 0)   {
    response = Docs.Documents.batchUpdate(
      { 'requests': requests }, doc.getId());
  }

我可以在循环中一遍又一遍地使用它,然后是 appendPageBreak()。


https://developers.google.com/apps-script/reference/document/table


有效,但我可能仍然需要使用 API 来设置表格的样式,我想知道我尝试的方法有什么问题。

function loadTable(body, inArr) {
  var inArrIdx = 0;
  var cellArr = [], rowArr = [];
  for (var r = 0 ; r < 5 ; r++ )    {
   for (var c = 0 ; c < 5 ; c++ )    {
    rowArr.push( inArr[inArrIdx] );
    inArrIdx++;
   }
    cellArr.push( rowArr );
    rowArr = [];
  }
  console.log('cellArr: ', cellArr );
  
  // Build a table from the array.
  body.appendTable(cellArr);
}
idfurw

来源是独占的,因为它只是在谈论 2 列表的索引规则

对于行,索引需要设置为每 5 个索引。对于列,索引需要设置为每 2 个索引。

概括:

  • 第一个单元格的索引: 5
  • 列的索引n index的+长度text列的n-1
  • 行索引n index +行最后一个单元格(列)的text+ 1(行中断)长度n
  requests.push ( { insertText: { text: "E2", location: { index: 24 } } } );
  requests.push ( { insertText: { text: "D2", location: { index: 22 } } } );
  requests.push ( { insertText: { text: "C2", location: { index: 20 } } } );
  requests.push ( { insertText: { text: "B2", location: { index: 18 } } } );
  requests.push ( { insertText: { text: "A2", location: { index: 16 } } } );
  requests.push ( { insertText: { text: "E1", location: { index: 13 } } } );
  requests.push ( { insertText: { text: "D1", location: { index: 11 } } } );
  requests.push ( { insertText: { text: "C1", location: { index: 09 } } } );
  requests.push ( { insertText: { text: "B1", location: { index: 07 } } } );
  requests.push ( { insertText: { text: "A1", location: { index: 05 } } } );

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Apps脚本将值设置为Google表格中的单元格?

如何使用列标题引用Google Apps脚本电子表格中的单元格

如何使用Google Apps脚本清除Google表格中的列?

使用Google表格脚本在列中查找空单元格

Google Apps脚本Q-在表格中可以设置公式(SUM)求和的单元格根据行而变化吗?

google apps脚本:单元格本身中的setNote()

Google脚本:使用单元格值填充多维数组

使用脚本在Google表格中定义单元格

如何使用Google Apps脚本在“ Google幻灯片”中合并2个表格单元格

如何使用Google文档中的Google Apps脚本设置表格中某行的背景颜色?

使用Apps脚本将非连续单元格从Google表格中的一列复制到另一列

如何使用Google Apps脚本在Google文档的表格单元内复制和编辑文本数据

Google表格-用于填充单元格IF列的脚本

Google表格+ Apps脚本:如何根据单元格的值按名称删除表格

使用自动填充功能链接Google表格中的两个单元格

Google表格-在脚本的OFFSET中使用单元格的值

使用Apps脚本返回到某些单元格并使其在Google表格中可编辑

使用Apps脚本将文本和图像添加到Google Doc中表格的特定单元格

使用Google表格中的输入来换行(Google Apps脚本)

Google表格:使用单元格引用填充系列

如何使用Google Apps脚本在电子表格的单元格中剪切文本?

如何使用Google Apps脚本向电子表格单元格添加链接

使用 Google Apps 脚本删除或替换 Google 表格单元格中的“未定义”字符串

使用空白行自动填充数据 - Google 表格 / Google Apps 脚本

使用 Google Apps 脚本转到工作表中包含今天日期的单元格

如何检查单元格中的日期+ 5 = 今天?(Google Apps 脚本 + Google 表格)

使用 Google Apps 脚本删除基于列中重复单元格的行

通过 Google Apps 脚本中的单元格循环 ID

Google 表格脚本:根据另一个单元格自动填充一个单元格,并使用脚本进行格式化