【发布时间】:2021-10-22 20:14:51
【问题描述】:
我想在一个空文档中添加一个 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 的表并得到以下错误: 错误 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