【问题标题】:Copy and share Google Doc with the same people复制并与同一个人共享 Google Doc
【发布时间】:2021-10-19 03:55:33
【问题描述】:

您好,我正在尝试复制一个模板并希望与同一个人共享它(类似于我们手动执行的方式)。我是个菜鸟,只做基本的事情,所以我无法解决

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)
    
  })}

【问题讨论】:

    标签: google-apps-script google-sheets google-docs


    【解决方案1】:

    我相信你的目标如下。

    • 您想与其他用户共享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);
        

    参考资料:

    【讨论】:

    • 谢谢田池。这是否意味着我必须在脚本中手动添加电子邮件地址? const emailAddresses = ["电子邮件地址 1", "电子邮件地址 2",,,];该模板与一些团队的所有者共享,这些所有者最终将在文档中签名,并且所有者可以更改。这个想法是在模板文档中添加编辑器,当我复制时,我会选择“与同一个人共享”复选框。
    • @Shamie 感谢您的回复。我带来的不便表示歉意。从您的回复中,我明白了same people。所以我更新了我的答案。你能确认一下吗?如果这没有用,我再次道歉。
    • 非常感谢,效果很好!
    • @Shamie 感谢您的回复。我很高兴你的问题得到了解决。也谢谢你。
    • 谢谢@Tanaike,明白了。我已经发布了一个新问题。
    猜你喜欢
    • 2016-06-15
    • 2023-03-08
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多