【问题标题】:Populate table cells in Google document with Google Apps Script使用 Google Apps 脚本填充 Google 文档中的表格单元格
【发布时间】:2021-10-22 20:14:51
【问题描述】:

我想在一个空文档中添加一个 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 调用失败并出现错误:无效的 requests[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() 都是文档正文的有效方法,所以我认为不需要在段落内添加带有表格的段落?

更多研究和田池也在这里回答

Unable to add text to new Google Doc via Docs API 我添加了 endOfSegmentLocation 并得到以下信息 GoogleJsonResponseException:对 docs.documents.batchUpdate 的 API 调用失败并出现错误: 收到无效的 JSON 有效负载。 'requests[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-script google-docs google-docs-api


    【解决方案1】:

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

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

    概括:

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

    【讨论】:

    • 非常感谢。我永远不会从文档中得到它。我确实查看了我使用的索引,即使使用 5x5,它们也是独一无二的。我想知道当网格变大时,5 和 2 的倍数是否会继续起作用。
    猜你喜欢
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多