首页 > 解决方案 > 使用 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 表并得到以下错误:错误 GoogleJsonResponseException:对 docs.documents.batchUpdate 的 API 调用失败并出现错误:无效请求 [5] .insertText:插入索引必须在里面现有段落的边界。您仍然可以通过插入换行符来创建新段落。

  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:对 docs.documents.batchUpdate 的 API 调用失败并出现错误:收到无效的 JSON 有效负载。“请求 [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);
}

标签: google-apps-scriptgoogle-docsgoogle-docs-api

解决方案


来源是排他性的,因为它只讨论 2 列表的索引规则

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

概括:

  • 第一个单元格的索引: 5
  • 列索引n +列index长度textn-1
  • 行索引n +行的最后一个单元格(列)的+ (行中断)index长度text1n
  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 } } } );

推荐阅读