首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复制并与相同人员共享Google Doc

复制并与相同人员共享Google Doc
EN

Stack Overflow用户
提问于 2021-10-19 03:55:34
回答 1查看 62关注 0票数 2

嗨,我试图复制一个模板,并希望它与相同的人共享(类似于我们如何手动做)。我完全是个菜鸟,只做最基本的事情,所以我不能修复。

代码语言:javascript
复制
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)
    
  })}
EN

回答 1

Stack Overflow用户

发布于 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.

在这种情况下,下面的修改如何?

发自:

代码语言:javascript
复制
//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

至:

代码语言:javascript
复制
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

  • 从你的问题中,我不能理解你是否想要以编辑器的身份共享文件。因此,如果要与查看器共享,请按如下方式进行修改。

代码语言:javascript
复制
- From

copy.addEditors(emailAddresses);

代码语言:javascript
复制
- To

copy.addViewers(emailAddresses);

参考文献:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69624925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档