我相信你的目标如下。
- 您希望从电子表格中检索值并将它们放入使用 Google Apps 脚本格式的文档中。
在你的情况下,下面的流程如何?
- 从电子表格中检索值并创建一个对象。
- 创建新的 Google 文档。
- 使用对象,每个值都以格式放入创建的文档中。
当这个流程反映在一个脚本中时,它变成如下。
示例脚本:
在此示例中,请将其复制并粘贴到包含值的 Google 电子表格的脚本编辑器中。并且,如果要更改title、sectionTitle 和head,请修改脚本。并且,运行函数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 个文档。会怎么样?
根据您评论中的新问题,我无法理解新文档的文件名以及title、sectionTitle 和head 的值。所以在这种情况下,请根据您的实际情况进行修改。
下面的示例脚本怎么样?
示例脚本:
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);
}
}
});
});
}