【问题标题】:Is there a function to convert a google doc to a google spreadsheet?是否有将谷歌文档转换为谷歌电子表格的功能?
【发布时间】:2019-07-16 05:06:47
【问题描述】:

我需要一种将数据从谷歌文档传输到谷歌电子表格的方法(我知道有点倒退)。我需要文档中的换行符相当于在下面开始一个新单元格。即谷歌文档中的每一行都有自己的单元格。

我尝试从 google doc 获取正文并将第一个单元格设置为该变量,但这只会将数据粘贴到单个 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-apps-script google-sheets google-docs


    【解决方案1】:
    • 您希望将文档的每一段放入在脚本中创建的电子表格的单元格中。
      • 根据您的脚本,您的 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);
    

    注意:

    • 在此脚本中,将忽略空段落。如果您想包含空段落,请像评论一样修改脚本。

    参考资料:

    如果上述脚本没有解决您的问题,我深表歉意。当时,为了正确理解您的情况,能否提供您想要的结果的示例文档和示例电子表格?当然,请从他们那里删除您的个人信息。

    【讨论】:

      猜你喜欢
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      相关资源
      最近更新 更多