【发布时间】:2021-10-25 06:38:27
【问题描述】:
我有以下代码来复制模板并与有权访问该模板的同一个人共享。
但是,我有两个问题:
- 我不希望他们在每次制作副本并与他们共享时都收到通知
- 我还想与其他人分享以进行编辑。可以编辑的人的电子邮件在 Google 工作表的 A 列中。这个新人也不应该收到通知。
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('Create My Checklist');
menu.addItem('New Checklist', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
//This value should be the id of your document template that we created in the last step
const googleDocTemplate = DriveApp.getFileById('1qRQ07PDmz1il9IftM9GIJfY37vTusfQZhhNS1BRELJQ');
//This value should be the id of the folder where you want your completed documents stored
const destinationFolder = DriveApp.getFolderById('1JufckhwXlAXDAE3_-f60lHQQ-jqe9Mx1')
//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();
const emailAddresses = googleDocTemplate.getEditors().map(e => e.getEmail()); // Added
//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[10]) return;
//Using the row data in a template literal, we make a copy of our template document in our destinationFolder
const copy = googleDocTemplate.makeCopy("eResignation Checklist - " + row[2] + " - " + row[4] + " - " + row[5], destinationFolder)
copy.addEditors(emailAddresses);
//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('{{Workday ID}}', row[1]);
body.replaceText('{{Full Name}}', row[2]);
body.replaceText('{{Management Level}}', row[3]);
body.replaceText('{{LoS}}', row[4]);
body.replaceText('{{Cost Centre}}', row[5]);
body.replaceText('{{Office 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)
Drive.Permissions.insert({role: "writer", type: "user", value: row[0]}, copy.getId(), {sendNotificationEmails: false, supportsAllDrives: true});
})}
【问题讨论】:
标签: google-apps-script google-drive-api google-docs