- 您希望将文档的每一段放入在脚本中创建的电子表格的单元格中。
- 根据您的脚本,您的 Google 文档只有文本。您的 Google 文档不包含表格、列表、图片等。
如果我的理解是正确的,那么这个修改呢?
模式一:
在此模式中,var body = openedFile.getBody().getText() 中的 body 被 \n 分割。然后,将这些值放入创建的电子表格中。
修改后的脚本:
var openedFile = DocumentApp.openById("###"); // Please set Document ID here.
var body = openedFile.getBody().getText();
var bodyAll = body.split("\n").reduce(function(ar, e) {
if (e) ar.push([e]); // If you want to include the empty paragraph, please modify to ar.push([e]);
return ar;
}, []);
var newSpreadsheet = SpreadsheetApp.create('TEST');
newSpreadsheet.getSheets()[0].getRange(1, 1, bodyAll.length, bodyAll[0].length).setValues(bodyAll);
模式 2:
在此模式中,段落从 Document 的正文中检索,并检索每个文本。然后,将这些值放入创建的电子表格中。
修改后的脚本:
var openedFile = DocumentApp.openById("###"); // Please set Document ID here.
var body = openedFile.getBody();
var bodyAll = body.getParagraphs().reduce(function(ar, e) {
var temp = e.getText();
if (temp) ar.push([temp]); // If you want to include the empty paragraph, please modify to ar.push([temp]);
return ar;
}, [])
var newSpreadsheet = SpreadsheetApp.create('TEST');
newSpreadsheet.getSheets()[0].getRange(1, 1, bodyAll.length, bodyAll[0].length).setValues(bodyAll);
注意:
- 在此脚本中,将忽略空段落。如果您想包含空段落,请像评论一样修改脚本。
参考资料:
如果上述脚本没有解决您的问题,我深表歉意。当时,为了正确理解您的情况,能否提供您想要的结果的示例文档和示例电子表格?当然,请从他们那里删除您的个人信息。