0

I have been struggling with this issue for quite some time without finding an answer. Basically, I have thousands (roughly 10k) of PDFs in a folder to which I need to grant viewing permission, each pdf to a unique email address. For example

PDF1 ---> to email A

PDF2 ---> to email B

and so on

I managed via script to get the URL for every single file and create a sheet that has basically 2 columns: URL and Email address. What I cannot manage to achieve is to give viewing permission to the email via app script.

This URL then will be sent to every email individually via another automation

Could you please help me to understand how to achieve it?

thanks a lot in advance

1 Answers1

0

Issue:

If I understand you correctly:

  • You have a list of Drive URLs and email addresses in a sheet (columns A-B).
  • You want to share each corresponding Drive file (URL in column A) with the email address in column B (with Viewing permission).

Solution:

If the above is correct, you can do the following:

Code snippet:

function addViewingPermissions() {
  const sheet = SpreadsheetApp.getActive().getSheetByName("SHEET_NAME"); // Change
  const data = sheet.getRange("A2:B"+sheet.getLastRow()).getValues();
  data.forEach(row => {
    const id = row[0].match(/[-\w]{25,}/)[0];
    const file = DriveApp.getFileById(id);
    file.addViewer(row[1]);
  })
}

Notes:

  • I am assuming that you are using a bound script.
  • Please change the SHEET_NAME from the code sample above to your actual sheet name.
Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • Thanks a lot, that works perfectly! Just one note, I would like not to send a notification to the email addresses. my understanding though is that a notification will always be sent for not-google-docs file. is that right or is there a way to do so? – mattia canzanella Apr 01 '21 at 22:16
  • thanks again @Iamblichus. What I was also seeking is to remove the sending of the notification from my side ( which I am sharing the file), so completely removing this message from the "sender side" ( I cannot ask 15k users to remove the notification from the "receiver side"). The reason being is that I don't want the receiving user to receive the email "someone has shared this doc" because my goal was to send later on a bespoke email with the link. would that be possible as far as you know? thanks – mattia canzanella Apr 02 '21 at 10:07
  • @mattiacanzanella I think you can do that if you share it using the API itself instead of `DriveApp` (see [Advanced Service](https://developers.google.com/apps-script/advanced/drive) and see [this related question](https://stackoverflow.com/q/22071187) - tldr, call `permissions: insert` and set `sendNotificationEmails=false` in your request). If you have doubts regarding that, I'd suggest you to post a new question. – Iamblichus Apr 02 '21 at 10:58