Reputation: 2788
I have a GSheet that is owned by a service account, and I would like to add an “on edit” trigger to receive real-time edit notifications.
So far I've tried/looked at:
Google Apps Script: The script cannot be run because it is owned by a service account. Please copy or transfer the project to a valid account before running.
Drive API push notifications - but that doesn’t give the per cell detail required, and regardless neither does it give a response faster enough; can be up to 3 mins according this SO post.
Changing the ownership of the GSheet - however other requirements require the GSheet to remain belonging to the service account.
Using the Script API - but it isn’t allowed to create triggers, and anyway service accounts can’t use the Script API.
Any other ideas, or is the fact Google simply doesn’t allow Apps Script to run under the service account a show stopper?”
Upvotes: 1
Views: 1404
Reputation: 201378
I believe your goal is as follows.
I confirmed that in the current stage, your goal can be directly achieved using the installable OnEdit trigger. The sample flow is as follows.
Please create a new Google Spreadsheet by the service account. And, please copy the Spreadsheet ID. In this case, the owner of the Spreadsheet is the service account.
And, please share this Spreadsheet with your Google account as a writer. This is an important point.
Please create a new standalone script with your account. In this case, the owner of the standalone script is your account.
And, please copy and paste the following script to the script editor. And, please set the Spreadsheet ID of Spreadsheet of the service account to spreadsheetId
and save the script.
function installTrigger() {
const spreadsheetId = "###"; // Please set the spreadsheet ID of your Spreadsheet.
const ss = SpreadsheetApp.openById(spreadsheetId);
ScriptApp.newTrigger("installedOnEdit").forSpreadsheet(ss).onEdit().create();
}
// In this pattern, please set your script in this function.
function installedOnEdit(e) {
e.range.setValue(JSON.stringify(e));
}
In order to install the OnEdit trigger, please run installTrigger()
. By this, the OnEdit trigger is installed to the function installedOnEdit
. By this, when a cell of the above Spreadsheet is edited, installedOnEdit
is run.
Please edit the cell in the Spreadsheet of the service account. By this, the script of your standalone script is run, and you can see the event object in the edited cell.
In the current stage, unfortunately, when the owner of Google Spreadsheet is the service account. The Google Apps Script cannot be run. By this, even when onEdit
function is put to the script editor, this script cannot be run. It seems that this is the current specification of the Google side. Unfortunately, in the current stage, your goal cannot be directly achieved. I think that this is the current answer to your question.
But, I thought that a workaround might be able to be proposed. Fortunately, the built-in functions of the Spreadsheet can be also used in the Spreadsheet created by the service account. I thought that this might be able to be used as a workaround.
When I saw your question, I remembered the following my posts.
I thought that when these posts are used, a workaround might be able to be proposed. In this answer, I would like to propose a workaround for achieving your goal. So, please think of this as the pseudo OnEdit trigger.
The flow of this workaround is as follows.
IMPORTXML
to another sheet (For example, it's "Sheet2".). IMPORTXML
requests to the Web Apps.
By this flow, when the cells in "Sheet1" are edited, the Google Apps Script of Web Apps can be run. This is the pseudo OnEdit trigger as this workaround.
In order to use Web Apps, please create Google Apps Script project by the account that the script can be run. In this case, as a sample situation, please create Google Apps Script project by your account instead of the service account.
Please copy and paste the following script to the script editor of the created Google Apps Script project.
function doGet(e) {
// do something
// Please set the script you want to use.
return ContentService.createTextOutput(`<result>Edited at ${new Date().toISOString()}</result>`).setMimeType(ContentService.MimeType.XML);
}
The detailed information can be seen at the official document.
https://script.google.com/macros/s/###/exec
.
Please prepare 2 sheets in Google Spreadsheet created by the service account. Those are "Sheet1" and "Sheet2".
Please put the following formula to "A1" of "Sheet2". In this case, please use your Web Apps URL. And, please confirm that the value is returned from the deployed Web Apps. If this cannot be done, please check the above flow again.
=IMPORTXML("https://script.google.com/macros/s/###/exec?values="&TEXTJOIN(",",TRUE,Sheet1!A:Z),"/result")
Please edit the cell on "Sheet1". When you use the above formula, please edit the cells in "A:Z". By this, the cell "A1" of "Sheet2" is refreshed, and by this refresh, Google Apps Script of Web Apps is run. This is the pseudo OnEdit trigger as this workaround.
Upvotes: 3