是否有将 google 文档转换为 google 电子表格的功能?
Is there a function to convert a google doc to a google spreadsheet?
我需要一种方法来将数据从 google 文档传输到 google 电子表格(我知道有点倒退)。我需要文档中的换行符等同于在下方开始一个新单元格。即 google 文档中的每一行都有自己的单元格。
我已经尝试从 google 文档中获取正文并将第一个单元格设置为该变量,但这只会将数据粘贴到单个 A1 单元格中(有点希望它类似于如果您将文档正文文本复制并粘贴到 A1 单元格中,它将一直填充 A 列中的单元格)
var body = openedFile.getBody().getText();
var newSpreadsheet = SpreadsheetApp.create('TEST').getId();
var testSpreadsheet = SpreadsheetApp.openById(newSpreadsheet);
testSpreadsheet.getRange('A1').setValue(bodyAll);
- 您想将文档的每个段落放入脚本中创建的电子表格的单元格中。
- 根据您的脚本,您的 Google 文档只有文本。您的 Google 文档不包含表格、列表、图像等。
如果我的理解是正确的,这个修改怎么样?
模式 1:
在此模式中,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);
模式二:
在此模式中,段落是从 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);
注:
- 在此脚本中,空段落将被忽略。如果要包含空段落,请像评论一样修改脚本。
参考文献:
如果上述脚本未能解决您的问题,我深表歉意。届时,为了正确了解你的情况,能否提供一份你想要的结果的sample Document和sample Spreadsheet?当然,请从他们那里删除您的个人信息。
我需要一种方法来将数据从 google 文档传输到 google 电子表格(我知道有点倒退)。我需要文档中的换行符等同于在下方开始一个新单元格。即 google 文档中的每一行都有自己的单元格。
我已经尝试从 google 文档中获取正文并将第一个单元格设置为该变量,但这只会将数据粘贴到单个 A1 单元格中(有点希望它类似于如果您将文档正文文本复制并粘贴到 A1 单元格中,它将一直填充 A 列中的单元格)
var body = openedFile.getBody().getText();
var newSpreadsheet = SpreadsheetApp.create('TEST').getId();
var testSpreadsheet = SpreadsheetApp.openById(newSpreadsheet);
testSpreadsheet.getRange('A1').setValue(bodyAll);
- 您想将文档的每个段落放入脚本中创建的电子表格的单元格中。
- 根据您的脚本,您的 Google 文档只有文本。您的 Google 文档不包含表格、列表、图像等。
如果我的理解是正确的,这个修改怎么样?
模式 1:
在此模式中,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);
模式二:
在此模式中,段落是从 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);
注:
- 在此脚本中,空段落将被忽略。如果要包含空段落,请像评论一样修改脚本。
参考文献:
如果上述脚本未能解决您的问题,我深表歉意。届时,为了正确了解你的情况,能否提供一份你想要的结果的sample Document和sample Spreadsheet?当然,请从他们那里删除您的个人信息。