【发布时间】:2021-01-15 18:11:00
【问题描述】:
我希望有人可以帮助我。 所以基本上这个函数有一个基于谷歌表单应用程序的触发器。 在表单中,我得到了一个包含 5 个名称的下拉菜单。 这些名称总是放在 Google 表格的第 1 行。 现在脚本使用工作表中的数据创建一个文档并将其放入特定文件夹(const destinationFolder) - 我现在的目标是根据从下拉列表中选择的名称更改destinationFolder - 所以我得到了5种不同的文本变体在应该导致 5 个不同目标文件夹的表格单元格(5 个名称)中。我如何定义它?非常感谢您帮助我!
这是现在的代码(我从 Jeff Everhart 的 YT 视频中得到它):
//This value should be the id of your document template that we created in the last step
const googleDocTemplate = DriveApp.getFileById('112VRkj6msylp-vwaSmHqLNQDosCBsP0HaMxxxx');
//This value should be the id of the folder where you want your completed documents stored
const destinationFolder = DriveApp.getFolderById('1wI4QEZ6iC3Ur9CgTZtg4DmMfkxxxxx')
//Here we store the sheet as a variable
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Data')
//Now we get all of the values as a 2D array
const rows = sheet.getDataRange().getValues();
//Start processing each spreadsheet row
rows.forEach(function(row, index){
//Here we check if this row is the headers, if so we skip it
if (index === 0) return;
//Here we check if a document has already been generated by looking at 'Document Link', if so we skip
it
if (row[14]) return;
//Using the row data in a template literal, we make a copy of our template document in our
destinationFolder
const copy = googleDocTemplate.makeCopy(`${row[2]} - Infos` , destinationFolder)
//Once we have the copy, we then open it using the DocumentApp
const doc = DocumentApp.openById(copy.getId())
//All of the content lives in the body, so we get that for editing
const body = doc.getBody();
//In these lines, we replace our replacement tokens with values from our spreadsheet row
body.replaceText('{{Zeitstempel}}', row[0]);
body.replaceText('{{NAME}}', row[2]);
body.replaceText('{{Intro}}', row[5]);
body.replaceText('{{Ziel}}', row[6]);
body.replaceText('{{Abgehalten}}', row[7]);
body.replaceText('{{Kooperation}}', row[8]);
body.replaceText('{{FinanzZiel}}', row[9]);
body.replaceText('{{Invest}}', row[10]);
body.replaceText('{{Invest20}}', row[11]);
body.replaceText('{{Notizen}}', row[3]);
body.replaceText('{{Startzeit}}', row[13]);
//We make our changes permanent by saving and closing the document
doc.saveAndClose();
//Store the url of our new document in a variable
const url = doc.getUrl();
//Write that value back to the 'Document Link' column in the spreadsheet.
sheet.getRange(index + 1, 15).setValue(url)
})
}```
And thats the new new new Code:
function createNewGoogleDocs() {
//This value should be the id of your document template that we created in the last step
const googleDocTemplate = DriveApp.getFileById('TemplateID');
//This value should be the id of the folder where you want your completed documents stored
// Define folder id per user
const userFolder = {
Alina: "1ImNRXlyaFPGEatDMgi4cQ2JhPxxxxx",
Cem: "1xHhxupTXD8KRYkSk2Lll31pDcQxxxxx",
Constantin: "1wI4QEZ6iC3Ur9CgTZtg4DmMfkxxxxx",
Marie: "1mvZbp-CQP-oWsVeVv7Cc2htXExxxxx",
Johanna: "FolderIDxxxxx2",
};
//Here we store the sheet as a variable
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Data')
//Now we get all of the values as a 2D array
const rows = sheet.getDataRange().getValues();
//Logger.log(rows);
//Start processing each spreadsheet row
rows.forEach(function(row, index){
//Here we check if this row is the headers, if so we skip it
if (index === 0) return;
Logger.log(row[14]);
//Here we check if a document has already been generated by looking at 'Document Link', if so we skip
if (row[14]) return;
//Using the row data in a template literal, we make a copy of our template document in our
Logger.log(row[1]);
Logger.log(userFolder[row[1]]);
const destinationFolder = DriveApp.getFolderById(userFolder[row[1]]);
const copy = googleDocTemplate.makeCopy(`${row[2]} - Infos` , destinationFolder)
//Once we have the copy, we then open it using the DocumentApp
const doc = DocumentApp.openById(copy.getId())
//All of the content lives in the body, so we get that for editing
const body = doc.getBody();
//In these lines, we replace our replacement tokens with values from our spreadsheet row
body.replaceText('{{Zeitstempel}}', row[0]);
body.replaceText('{{NAME}}', row[2]);
body.replaceText('{{Intro}}', row[5]);
body.replaceText('{{Ziel}}', row[6]);
body.replaceText('{{Abgehalten}}', row[7]);
body.replaceText('{{Kooperation}}', row[8]);
body.replaceText('{{FinanzZiel}}', row[9]);
body.replaceText('{{Invest}}', row[10]);
body.replaceText('{{Invest20}}', row[11]);
body.replaceText('{{Notizen}}', row[3]);
body.replaceText('{{Startzeit}}', row[13]);
//We make our changes permanent by saving and closing the document
doc.saveAndClose();
//Store the url of our new document in a variable
const url = doc.getUrl();
//Write that value back to the 'Document Link' column in the spreadsheet.
sheet.getRange(index + 1, 15).setValue(url)
})
}
【问题讨论】:
-
欢迎来到 StackOverflow!您自己是否已经研究过 Google App Script 基础知识?你在哪里卡住了?抱歉,如果我误解了您的问题,但请记住,当您已经尝试过一段特定的代码时,这个网站更多的是针对具体问题,所以以防万一您更卡在“我如何编程”完全”阶段(如果不是这种情况,我深表歉意),那么首先投入更多时间以获得更好的回复以及稍后更详细的查询可能会有所帮助。
标签: google-apps-script google-sheets