Home > OS >  How to share document with app script with sending a notification and without a notification?
How to share document with app script with sending a notification and without a notification?

Time:11-29

I'm new to app-script, I was wondering if anyone can help me with this. Since this is not a common case, I couldn't find a similar case like this in here.

I was assigned to send a file to the list of emails using google app script. The list is like this: enter image description here

They give me a list of spreadsheets that they want to share with the email. For example, link number 1 would be sent to the emails that were listed in the same row as link number 1, link number 2 would be sent to the email that was listed in the same row as link number 2, and so on.

The second case is, I would like to have 2 versions of the script:

  1. without giving notification via email
  2. with notification via email

Could anyone advise what script should I use? Thank you so much!

Link to the samples you can use for reference.

CodePudding user response:

I believe your goal is as follows.

  • You want to share the Spreadsheet files with users by retrieving the URL of the Spreadsheet and the emails.
  • You want to select for sending the notification emails when the Spreadsheet is shared.
  • You want to achieve this using Google Apps Script.

In this case, how about the following sample script?

Sample script:

In this script, Drive API is used. So please enable Drive API at Advanced Google services.

function myFunction() {
  const notification = false; // When this value is true, the notification email is sent.
  const sheetName = "Spreadsheet"; // Please set the sheet name.

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  sheet
    .getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn())
    .getValues()
    .forEach(([url, ...emails]) => {
      const spreadsheetId = SpreadsheetApp.openByUrl(url).getId();
      emails.filter(String).forEach(e => Drive.Permissions.insert({ role: "writer", type: "user", value: e }, spreadsheetId, { sendNotificationEmails: notification }));
    });
}

References:

  • Related