嗨,我试图复制一个模板,并希望它与相同的人共享(类似于我们如何手动做)。我完全是个菜鸟,只做最基本的事情,所以我不能修复。
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('Create My Checklist');
menu.addItem('New Checklist', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
//id of the document template
const googleDocTemplate = DriveApp.getFileById('1qRQ07PDmz1il9IftM9GIJfY37vTusfQZhhNS1BRELJQ');
//id of the folder where the completed documents stored
const destinationFolder = DriveApp.getFolderById('1JufckhwXlAXDAE3_-f60lHQQ-jqe9Mx1')
//Here we store the sheet as a variable
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Data')
//get all of the values as a 2D array
const rows = sheet.getDataRange().getValues();
//Start processing each spreadsheet row
rows.forEach(function(row, index){
//check if this row is the headers, if yes, skip it
if (index === 0) return;
//check if a document has already been generated by looking at 'Document Link', if yes, skip it
if (row[10]) return;
//Using the row data in a template literal, make a copy of the template document in the destinationFolder
const copy = googleDocTemplate.makeCopy("My Checklist - " + row[2] + " - " + row[4] + " - " + row[5], destinationFolder)
//Copy, then open it using the DocumentApp
const doc = DocumentApp.openById(copy.getId())
//Get contents for editing
const body = doc.getBody();
//replace token with values from spreadsheet row
body.replaceText('{{Employee ID}}', row[1]);
body.replaceText('{{Name}}', row[2]);
body.replaceText('{{Level}}', row[3]);
body.replaceText('{{Department}}', row[4]);
body.replaceText('{{Business Unit}}', row[5]);
body.replaceText('{{Location}}', row[6]);
//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, 11).setValue(url)
})}发布于 2021-10-19 04:37:19
我相信你的目标如下。
const copy = googleDocTemplate.makeCopy("My Checklist - " + row[2] + " - " + row[4] + " - " + row[5], destinationFolder)的复制文件。the same people (similar to how we do it manually)的意思是share with the same people.在这种情况下,下面的修改如何?
发自:
//Start processing each spreadsheet row
rows.forEach(function(row, index){
//check if this row is the headers, if yes, skip it
if (index === 0) return;
//check if a document has already been generated by looking at 'Document Link', if yes, skip it
if (row[10]) return;
//Using the row data in a template literal, make a copy of the template document in the destinationFolder
const copy = googleDocTemplate.makeCopy("My Checklist - " + row[2] + " - " + row[4] + " - " + row[5], destinationFolder)
//Copy, then open it using the DocumentApp至:
const emailAddresses = googleDocTemplate.getEditors().map(e => e.getEmail()); // Added
//Start processing each spreadsheet row
rows.forEach(function(row, index){
//check if this row is the headers, if yes, skip it
if (index === 0) return;
//check if a document has already been generated by looking at 'Document Link', if yes, skip it
if (row[10]) return;
//Using the row data in a template literal, make a copy of the template document in the destinationFolder
const copy = googleDocTemplate.makeCopy("My Checklist - " + row[2] + " - " + row[4] + " - " + row[5], destinationFolder)
copy.addEditors(emailAddresses); // Added
//Copy, then open it using the DocumentApp- Fromcopy.addEditors(emailAddresses);
- Tocopy.addViewers(emailAddresses);
参考文献:
https://stackoverflow.com/questions/69624925
复制相似问题