【问题标题】:How to convert table from google sheet into paragraph in google document?如何将表格从谷歌表格转换为谷歌文档中的段落?
【发布时间】:2021-10-28 07:17:55
【问题描述】:

我是谷歌应用脚​​本的新手。我正在尝试使用 google app 脚本创建一个 google 文档,以帮助我将表格(电子表格)中的数据转换为 google doc 中的段落。

这是data source的样本,这就是我想要的expected output

我应该使用什么脚本来实现这一点?

感谢您回答问题。

【问题讨论】:

    标签: javascript google-apps-script google-sheets google-docs


    【解决方案1】:

    我相信你的目标如下。

    • 您希望从电子表格中检索值并将它们放入使用 Google Apps 脚本格式的文档中。

    在你的情况下,下面的流程如何?

    1. 从电子表格中检索值并创建一个对象。
    2. 创建新的 Google 文档。
    3. 使用对象,每个值都以格式放入创建的文档中。

    当这个流程反映在一个脚本中时,它变成如下。

    示例脚本:

    在此示例中,请将其复制并粘贴到包含值的 Google 电子表格的脚本编辑器中。并且,如果要更改titlesectionTitlehead,请修改脚本。并且,运行函数myFunction

    function myFunction() {
      const title = "DOCUMENT TITLE";
      const sectionTitle = "SECTION TITLE";
      const head = ["ID: ", "EMPLOYEE NAME: "];
    
      // 1. Retrieve values from Spreadsheet and create an object.
      const [headers, ...rows] = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getDataRange().getValues();
      const res = rows.map((r) => headers.reduce((o, h, j) => Object.assign(o, { [h]: r[j] }), {}));
    
      // 2. Create new Google Document.
      const doc = DocumentApp.create("temp");
    
      // 3. Using the object, each value is put to the created Document with the format.
      const lineBreak = _ => body.appendParagraph("").editAsText().setBold(false).setFontSize(11);
      const body = doc.getBody()
      body.appendParagraph(title).setHeading(DocumentApp.ParagraphHeading.HEADING1).editAsText().setBold(true).setFontSize(20);
      lineBreak();
      res.forEach(e => {
        headers.forEach((h, j) => {
          if (e[h]) {
            if (j < 2) {
              body.appendParagraph(head[j] + e[h]).editAsText().setBold(0, head[j].length - 1, true).setFontSize(11);
              if (j == 1) {
                lineBreak();
                body.appendParagraph(sectionTitle).editAsText().setBold(true).setFontSize(14);
                lineBreak();
              }
            } else if (j == 2) {
              body.appendParagraph(e[h]).setHeading(DocumentApp.ParagraphHeading.HEADING2).editAsText().setBold(true).setFontSize(12);
              lineBreak();
            } else {
              body.appendParagraph(e[h]);
              lineBreak();
            }
          }
        });
      });
    }
    
    • 运行此脚本时,会创建一个新的 Google 文档,并将电子表格中的值放入文档中。您可以在根文件夹中看到它。

    注意:

    • 此示例脚本来自您的示例 Google 电子表格和 Google 文档。更改这些内容后,可能无法使用此脚本。请注意这一点。

    参考资料:

    补充:

    关于您的以下新问题,

    谢谢!这真的很有帮助。我想知道是否假设我想为 Johnny Depp 创建 1 个文档,为 Michael Page 创建 1 个文档,假设我在表格中有更多的姓名列表,我想为每个人创建 1 个文档。会怎么样?

    根据您评论中的新问题,我无法理解新文档的文件名以及titlesectionTitlehead 的值。所以在这种情况下,请根据您的实际情况进行修改。

    下面的示例脚本怎么样?

    示例脚本:

    function myFunction() {
      const title = "DOCUMENT TITLE";
      const sectionTitle = "SECTION TITLE";
      const head = ["ID: ", "EMPLOYEE NAME: "];
    
      const [headers, ...rows] = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getDataRange().getValues();
      const res = rows.map((r) => headers.reduce((o, h, j) => Object.assign(o, { [h]: r[j] }), {}));
    
      const lineBreak = body => body.appendParagraph("").editAsText().setBold(false).setFontSize(11);
      let body;
      res.forEach((e, i) => {
        headers.forEach((h, j) => {
          if (e[h]) {
            if (j < 2) {
              if (j == 0) {
                const doc = DocumentApp.create(e["Legal Name"]);
                body = doc.getBody()
                body.appendParagraph(title).setHeading(DocumentApp.ParagraphHeading.HEADING1).editAsText().setBold(true).setFontSize(20);
                lineBreak(body);
                body.appendParagraph(head[j] + e[h]).editAsText().setBold(0, head[j].length - 1, true).setFontSize(11);
              } else if (j == 1) {
                body.appendParagraph(head[j] + e[h]).editAsText().setBold(0, head[j].length - 1, true).setFontSize(11);
                lineBreak(body);
                body.appendParagraph(sectionTitle).editAsText().setBold(true).setFontSize(14);
                lineBreak(body);
              }
            } else if (j == 2) {
              body.appendParagraph(e[h]).setHeading(DocumentApp.ParagraphHeading.HEADING2).editAsText().setBold(true).setFontSize(12);
              lineBreak(body);
            } else {
              body.appendParagraph(e[h]);
              lineBreak(body);
            }
          }
        });
      });
    }
    

    【讨论】:

    • 谢谢!这真的很有帮助。我想知道是否假设我想为 Johnny Depp 创建 1 个文档,为 Michael Page 创建 1 个文档,假设我在表格中有更多的姓名列表,我想为每个人创建 1 个文档。会怎么样?
    • @Nadila 感谢您的回复。关于您的新问题,我在回答中又添加了一个示例脚本。你能确认一下吗?如果这没有用,我深表歉意。
    • 非常感谢!正如预期的那样:)
    • Nadila,请将 Tanaike 提交的解决方案帖子标记为“最佳答案”。这有助于贡献者社区轻松查看已完全解决的问题。它可以帮助未来的网站访问者快速找到最相关的答案。 (对于像 Tanaike 这样的人来说,这是一个很小的感激之情,他显然花了很多时间为你制定这样的解决方案。)
    • @Erik Tyler 感谢您的支持。
    猜你喜欢
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    相关资源
    最近更新 更多