如何将表格从Google表格转换为Google文档中的段落? [英] How to convert table from google sheet into paragraph in google document?

查看:48
本文介绍了如何将表格从Google表格转换为Google文档中的段落?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是data source的样本,这是我想要的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文档。更改后,此脚本可能无法使用。请注意此点。

引用:

添加:

关于您的以下新问题

谢谢!这真的很有帮助。我在想,假设我想为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);
        }
      }
    });
  });
}

这篇关于如何将表格从Google表格转换为Google文档中的段落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆