嗨,我正在嘗試復制模板并希望與相同的人共享它(類似于我們手動執行的方式)。我是個菜鳥,只做基本的東西,所以我無法解決
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)
})}
uj5u.com熱心網友回復:
我相信你的目標如下。
- 您想
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
從您的問題中,我無法理解您是否想以編輯者的身份共享該檔案。所以如果你想分享給觀眾,請修改如下。
從
copy.addEditors(emailAddresses);到
copy.addViewers(emailAddresses);
參考:
- addEditors(emailAddresses)
- 添加查看者(電子郵件地址)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/326441.html
