0

To share spreadsheets and documents it is easy to write a script:

    var sheet = SpreadsheetApp.openById("xxxxxxxxxxxxxxxxxxxxxxxx");
    sheet.addViewer("email@gmail.com");

A libray ia a stand alone Google Apps Script file. Is there a comparable script to share such a file?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Rob
  • 157
  • 2
  • 8

1 Answers1

1

You can achieve it using DriveApp as follows.

var file = DriveApp.getFileById("xxxxxxxxxxxxxxxxxxxxxxxx");
file.addViewer("email@gmail.com");

If you want to share files as ANYONE, you can use the following sample.

var file = DriveApp.getFileById("xxxxxxxxxxxxxxxxxxxxxxxx");
file.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);

References :

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks for helping me again! Interesting solution. Is it not needed for working of the functions in the library to add the library on the google drive of "email@gmail.com"? If this is true the account "email@gmail.com" does not see the library, that means a better protection of my scripts in the library. Than i am even more happy! – Rob Nov 22 '17 at 12:57
  • @user3887038 If you want to use the function of the script as a GAS library, the users who use the library is not required to copy the script on own Drive. After it shared the script, the user of "email@gmail.com" can use the function by installing the function to the script as a library. Reference is https://developers.google.com/apps-script/guides/libraries If I misunderstand your comment, I'm sorry. – Tanaike Nov 23 '17 at 01:16
  • Indeed my script is a GAS library. So to give anyone to that file I don't need to share the library anymore. I searched and found this reference https://stackoverflow.com/questions/13741173/source-code-of-libraries-used-by-webapps-visible-to-everybody. – Rob Nov 24 '17 at 07:08
  • @user3887038 I'm sorry for not being helpful. – Tanaike Nov 24 '17 at 08:28
  • In contrary you were very helpfull, At first i tried var file = DriveApp.getFileById("xxxxxxxxxxxxxxxxxxxxxxxx"); file.addViewer("email@gmail.comsetSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW); – Rob Nov 25 '17 at 15:42
  • That works well, but your solution was even better because the library is not vissible at all for customer email@gmail.com. At the and I don't need this script at all, by sharing my library direct tot anyone with a link., My library is on this way better protected. So again thank you very much. – Rob Nov 25 '17 at 15:51
  • @user3887038 Thank you for your additional information. – Tanaike Nov 25 '17 at 22:58