我想将一个 5x5 的表格添加到一个空文档中。最终,我想在几页中的每一页上添加一个。文档很神秘,但我从 Tanaike 找到了这个很好的例子:
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);
}
来源是独占的,因为它只是在谈论 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] 删除。
我来说两句