google-sheetsgoogle-apps-scriptgoogle-drive-api

Drive.Permissions.create is not a function


In a management file, I try to be able to give editor rights by script to addresses entered in a tab: the emails for which I want to update the editor rights are added in columns C and D of the ClubUrls tab

When launching the script, I get the error

Encountered an error: TypeError: Drive.Permissions.create is not a function

My file : https://docs.google.com/spreadsheets/d/1CCvy7rp0jd7ZXYGjBqfoOQTnYqtt6BnaOT_KE8fVg68/edit?usp=sharing

Using Drive allows me to choose whether to send notification email.

My script :

//----- Gestion des éditeurs/commentateurs/lecteurs
function partagerFichiersEditeur() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("ClubUrls");
  let data = sheet.getDataRange().getValues();

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

  //Process sheet file data
  dataToBeProcessed.forEach(data => {

    let fileId = data[1]; //les URL sont dans la 2ème colonne
    console.log(fileId)
    try {
      data[2].forEach(destinataireEd1 => {  //les éditeurs1 sont dans la 3ème colonne
        destinataireEd1 != '' && (Drive.Permissions.create({ role: "writer", type: "user", emailAddress: destinataireEd1 }, fileId, { sendNotificationEmail: false }), console.log(`Successfully shared file ID Writer : (${fileId}) to ${destinataireEd1}`))
      });

      data[3].forEach(destinataireEd2 => {  //les éditeurs2 sont dans la 4ème colonne
        destinataireEd2 != '' && (Drive.Permissions.create({ role: "writer", type: "user", emailAddress: destinataireEd2 }, fileId, { sendNotificationEmail: false }), console.log(`Successfully shared file ID Writer : (${fileId}) to ${destinataireEd2}`));
      });
      
      data[4].forEach(destinataireLect1 => {  //les Lecteur1 sont dans la 5ème colonne
        destinataireLect1 != '' && (Drive.Permissions.create({ role: "reader", type: "user", emailAddress: destinataireLect1 }, fileId, { sendNotificationEmail: false }), console.log(`Successfully shared file ID Reader : (${fileId}) to ${destinataireLect1}`))
      });

      data[5].forEach(destinataireLect2 => {  //les Lecteur2 sont dans la 6ème colonne
        destinataireLect2 != '' && (Drive.Permissions.create({ role: "reader", type: "user", emailAddress: destinataireLect2 }, fileId, { sendNotificationEmail: false }), console.log(`Successfully shared file ID Reader : (${fileId}) to ${destinataireLect2}`))
      });

      /*data[7].forEach(destinataireCom1 => {   //les Commentateur1 sont dans la 8ème colonne
        destinataireCom1 != '' && (Drive.Permissions.create({ role: "commenter", type: "user", emailAddress: destinataireCom1 }, fileId, { sendNotificationEmail: false }), console.log(`Successfully shared file ID: (${fileId}) to ${destinataireCom1}`))
      });

      data[8].forEach(destinataireCom2 => {  //les Commentateur2 sont dans la 9ème colonne
        destinataireCom2 != '' && (Drive.Permissions.create({ role: "commenter", type: "user", emailAddress: destinataireCom2 }, fileId, { sendNotificationEmail: false }), console.log(`Successfully shared file ID: (${fileId}) to ${destinataireCom2}`))
      });
      */


    } catch (e) {
      console.log('Encountered an error: \n' + e)
    }

  })
}

Solution

  • Use addEditor()

    You might try this script as an alternative solution.

    If you only need to assign editor permission, you can replace Drive.Permissions.create with DriveApp methods, which support addEditor() , which have specific permission. The DriveApp does not need to enable the Advanced Drive API.

    Change:

    data[2].forEach(destinataireEd1 => {  
            destinataireEd1 != '' && (Drive.Permissions.create({ role: "writer", type: "user", emailAddress: destinataireEd1 }, fileId, { sendNotificationEmail: false }), console.log(`Successfully shared file ID Writer : (${fileId}) to ${destinataireEd1}`))
          });
    
    data[3].forEach(destinataireEd2 => {  
            destinataireEd2 != '' && (Drive.Permissions.create({ role: "writer", type: "user", emailAddress: destinataireEd2 }, fileId, { sendNotificationEmail: false }), console.log(`Successfully shared file ID Writer : (${fileId}) to ${destinataireEd2}`));
          });
    

    To:

    data[2].forEach(destinataireEd1 => {  
           destinataireEd1 != '' && (file.addEditor(destinataireEd1), console.log(`Successfully shared file ID Writer : (${fileId}) to ${destinataireEd1}`));
         });
    data[3].forEach(destinataireEd2 => {  
           destinataireEd2 != '' && (file.addEditor(destinataireEd2), console.log(`Successfully shared file ID Writer : (${fileId}) to ${destinataireEd2}`));
         });
    

    Full Script:

    //----- Gestion des éditeurs/commentateurs/lecteurs
    function partagerFichiersEditeur() {
     const ss = SpreadsheetApp.getActiveSpreadsheet();
     const sheet = ss.getSheetByName("ClubUrls");
     let data = sheet.getDataRange().getValues();
     const dataToBeProcessed = data.map((row, i) => {
       return i != 0 && row.map(cell => {
         try {
           return new RegExp("d\/(.*)\/", "gm").exec(cell)[1]; 
         } catch {
           return cell.trim().split(";") 
         }
       })
     }).filter(x => x); 
      
     dataToBeProcessed.forEach(data => {
       let fileId = data[1]; 
       console.log(fileId)
       try {
         let file = DriveApp.getFileById(fileId);
        
         data[2].forEach(destinataireEd1 => {  
           destinataireEd1 != '' && (file.addEditor(destinataireEd1), console.log(`Successfully shared file ID Writer : (${fileId}) to ${destinataireEd1}`));
         });
         data[3].forEach(destinataireEd2 => {  
           destinataireEd2 != '' && (file.addEditor(destinataireEd2), console.log(`Successfully shared file ID Writer : (${fileId}) to ${destinataireEd2}`));
         });
        
         /*data[4].forEach(destinataireLect1 => {  
           destinataireLect1 != '' && (file.addViewer(destinataireLect1), console.log(`Successfully shared file ID Reader : (${fileId}) to ${destinataireLect1}`));
         });
         data[5].forEach(destinataireLect2 => {  
           destinataireLect2 != '' && (file.addViewer(destinataireLect2), console.log(`Successfully shared file ID Reader : (${fileId}) to ${destinataireLect2}`));
         });
         */
        
       } catch (e) {
         console.log('Encountered an error: \n' + e)
       }
     })
    }
    

    References:

    Class DriveApp

    Class Document