如何将 table 从 google sheet 转换为 google 文档中的段落?

How to convert table from google sheet into paragraph in google document?

我是 google 应用脚本的新手。我试图使用 google 应用程序脚本创建一个 google 文档,以帮助我将 table(来自电子表格)中的数据转换为 google 文档中的段落。

这是我想要的data source, and this is the expected output的样本。

我应该使用什么脚本来做到这一点?

感谢您回答问题。

我相信你的目标如下。

  • 您想要从电子表格中检索值并将它们放入使用 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 文档。当这些改变时,这个脚本可能无法使用。请注意这一点。

参考文献:

已添加:

关于您接下来的新问题,

Thanks! This is really helpful. I was wondering if let's say I want to create 1 document for Johnny Depp, and 1 document for Michael Page, and let's say I have more list of names in the table and I want to create 1 document for each person. How will it be?

根据您评论中的新问题,我无法理解新文档的文件名以及 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);
        }
      }
    });
  });
}