Mike Warren
Mike Warren

Reputation: 3866

Creating a Google Sheet on the end-user's account

A couple days ago, I wrote an app that creates spreadsheets for measuring potholes. Yesterday, I had someone test that app in front of me, and from what I saw, past the other bugs, the user got some "access request" screen. Hours later, I went to Google Sheets on my account and saw that the sheet the app generated got put on my account and not theirs. I go to Gmail, and got an e-mail saying that they're requesting access to the spreadsheet.

How do I either create it under their account or make sure that they automatically have access to, and edit privilege for, it?

Upvotes: 2

Views: 155

Answers (1)

user6655984
user6655984

Reputation:

There are two choices:

(a) When deploying the app, set the parameter "Execute the app as" to "User accessing the web app". Then whatever the app does will be under that user's authorization, in particular they will own any spreadsheets created by the app.

(b) Execute web app as you (the developer), but add the user as an editor by the addEditor method. A straightforward attempt would be

function doGet() {
  var ss = SpreadsheetApp.create("new spreadsheet");
  var user = Session.getActiveUser();
  ss.addEditor(user);
  return HtmlService.createHtmlOutput("<h1>Success!</h1>");
}

This should work if both developer and user are in the same G Suite domain. But in general it will not work, because the app is not authorized to know user's identity: see documentation.

A workaround is to ask the user to enter their email address (add another field to your form to collect it), and add them as an editor using it.

  ss.addEditor(email);

Upvotes: 1

Related Questions