Hi I am trying to copy a template and want it to be shared with the same people (similar to how we do it manually). I am a total noob and get by with just doing the basic stuff, so I am not able to fix
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)
})}
CodePudding user response:
I believe your goal is as follows.
- You want to share the copied file of
const copy = googleDocTemplate.makeCopy("My Checklist - " row[2] " - " row[4] " - " row[5], destinationFolder)
with other users.the same people (similar to how we do it manually)
meansshare with the same people
.
In this case, how about the following modification?
From:
//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
To:
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
From your question, I couldn't understand whether you wanted to share the file as the editor. So if you wanted to share it with the viewer, please modify it as follows.
From
copy.addEditors(emailAddresses);
To
copy.addViewers(emailAddresses);