禁用自动发送共享文档通知

问题描述 投票:0回答:1

我有一个包含录音的谷歌表格文件;特别是,A 列包含我想要与我在 B 列和 C 列中输入的收件人共享的 URL。我想将这些收件人放入相应 URL 的编辑器中,但不发送通知。

我尝试这个代码:

function partagerFichiers() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("Feuille 1");
  let data = sheet.getDataRange().getValues();
  let fichierUrl, destinataireEd1, destinataireEd2, destinataireCom, destinataireLec;
  for (i=1;i<=data.length;i++) {
    if (data[i].length > 1 && data[i][0] !== "") { //on récupère la 1ère colonne
      fichierUrl = data[i][0];}
    if (data[i].length > 1 && data[i][2] !== "") {
      destinataireEd1 = data[i][2].split(";").map(function(item) { return item.trim(); });} //on récupère la 3ème colonne
    if (data[i].length > 1 && data[i][3] !== "") {
      destinataireEd2 = data[i][3].split(";").map(function(item) { return item.trim(); });} //on récupère la 4ème colonne

    try {
      let fichierId = fichierUrl.replace(/.*\/d\//, '').replace(/\/.*/, '');
      let fichier = DriveApp.getFileById(fichierId);
      destinataireEd1.forEach(function(mail) { fichier.addEditor(mail); });
      destinataireEd1.forEach(destinataireEd1 => {
      Drive.Permissions.create({ role: "writer", type: "user", emailAddress: destinataireEd1 }, fichier, { sendNotificationEmail: false });});
      destinataireEd2.forEach(function(mail) { fichier.addEditor(mail); });
    } catch (error) {
      Logger.log("Erreur lors de la récupération du fichier pour l'url: " + fichierUrl);
      Logger.log("Erreur :" + error.message);
    }
  }
}

但是我收到几个错误:“错误:驱动器未定义”和“错误:无效参数:permission.value”(对于最后一个,我认为这是因为我没有某些记录的返回地址)

谢谢

google-sheets google-apps-script google-api google-drive-api
1个回答
0
投票

如果您想与收件人共享文件而不让他们收到电子邮件通知,您可以使用适用于 Google Apps 脚本的 Advanced Drive Service Drive API 来实现。我还修改了您现有的脚本,使其更加简洁和直接,使用更少的 for 循环和条件语句。

首先,您需要在 Apps 脚本编辑器中启用 Drive API Advanced Services,如下所示:

调整脚本

注意:如果您在使用示例脚本时遇到实际电子表格文件的问题,如果您可以从您的端共享虚拟工作表,以便我们可以正确复制您的设置,将会很有帮助。

function shareFiles() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("Feuille 1");
  let data = sheet.getDataRange().getValues();

  const dataToBeProcessed = data.map(row => {
    try {
      return [
        new RegExp("d\/(.*)\/", "gm").exec(row[0])[1], //extract file ID from the link
        row[2].trim().split(";"), //split email addresses for recipient 1
        row[3].trim().split(";") //split email addresses for recipient 2
      ]
    } catch (e) {
      return null; //skip non-related cells with values
    }
  }).filter(x => x); //filter null values


  //Process sheet file data
  dataToBeProcessed.forEach(data => {
    let fileId = data[0];

    data[1].forEach(editor1Recipient => {
      Drive.Permissions.create({ role: "writer", type: "user", emailAddress: editor1Recipient }, fileId, { sendNotificationEmail: false })
    });

    data[2].forEach(editor2Recipient => {
      Drive.Permissions.create({ role: "writer", type: "user", emailAddress: editor2Recipient }, fileId, { sendNotificationEmail: false })
    });

  })
}
© www.soinside.com 2019 - 2024. All rights reserved.