Search code examples
google-apps-scriptgoogle-sheetsweb-applicationspermissionssharing

How get sharable link of google spreadsheet?


I have created a google spreadsheet using web-apps-script and want to assign it a view-only access.

how can I do so I tried many different method but it does not work and keeps giving me errors.

For example:- enter image description here

I want any method to share my file to other user either by getting a shareable link or adding it into a public folder.

First I tried sharing it using script but received error :-

var ssNew = SpreadsheetApp.create("allowance "+(new Date));
var ss = SpreadsheetApp.openByUrl(ssNew.getUrl());
var fileId = ss.getId();
DriveApp.getFileById(fileId).addEditor("[email protected]");

Then I tried moving spreadsheet into public folder but nothing works and I receive error:-

var ssNew = SpreadsheetApp.create("allowance "+(new Date));
var folder=DriveApp.getFolderById('0AOexqtJ8UhRxPWUyk9PVxxA');
var id = ssNew.getId();
var file = DriveApp.getFileById(id);
folder.addFile(file);

Solution

  • You need to update your permissions:

    Taking into account the error you're getting, here's what I think is happening:

    • You first deployed your web app with a Project version, and authorized the web app to access user's data. In this first version of the app, DriveApp.getFileById was not part of the code, so you didn't authorize the scopes required for calling this method.
    • After deploying the web app, you modified the script and added DriveApp.getFileById, which requires either https://www.googleapis.com/auth/drive.readonly or https://www.googleapis.com/auth/drive.
    • After modifying the script you did not deploy a new version of the script (you would have been prompted to Review Permissions and authorize DriveApp.getFileById). Instead, you're probably trying to test the web app via latest code (URL ending in /dev), which doesn't necessarily require you to update your permissions.

    To solve this, create a new Project version and review your permissions as you will be requested (or update any other settings for the web app, like Execute the app as, which can also prompt the authorization popup).