Andrew Roberts
Andrew Roberts

Reputation: 2788

Creating an onEdit trigger on a sheet owned by a service account

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.

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

Answers (1)

Tanaike
Tanaike

Reputation: 201378

I believe your goal is as follows.

  • You have a Google Spreadsheet created by the service account.
  • You want to use the OnEdit trigger on this Spreadsheet.

Updated on January 25, 2023:

I confirmed that in the current stage, your goal can be directly achieved using the installable OnEdit trigger. The sample flow is as follows.

Flow

1. Create a new Google Spreadsheet by service account.

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.

2. Create a standalone script by your account.

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));
}

3. Install the OnEdit trigger.

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.

4. Testing.

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.

References:

Old post:

Issue and workaround:

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.

  1. Prepare Web Apps.
    • This Web Apps is deployed at the Google Apps Script project created by an account that the script can be run. Please be careful about this.
  2. When the pseudo OnEdit trigger is used in "Sheet1", put IMPORTXML to another sheet (For example, it's "Sheet2".). IMPORTXML requests to the Web Apps.
    • This Spreadsheet is the Spreadsheet created by the service account.
  3. When the cells in "Sheet1" are edited, the formula in "Sheet2" is run.

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.

Usage:

1. Create Google Apps Script for Web Apps.

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.

2. Sample script:

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);
}

3. Deploy Web Apps.

The detailed information can be seen at the official document.

  1. On the script editor, at the top right of the script editor, please click "click Deploy" -> "New deployment".
  2. Please click "Select type" -> "Web App".
  3. Please input the information about the Web App in the fields under "Deployment configuration".
  4. Please select "Me" for "Execute as".
    • This is the importance of this workaround.
  5. Please select "Anyone" for "Who has access".
    • In your situation, I thought that this setting might be suitable.
  6. Please click "Deploy" button.
  7. Copy the URL of the Web App. It's like https://script.google.com/macros/s/###/exec.

3. Testing.

  1. Please prepare 2 sheets in Google Spreadsheet created by the service account. Those are "Sheet1" and "Sheet2".

  2. 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")
    
    • This formula is a sample formula. So, please modify the range and formula for your actual situation.
  3. 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.

Note:

  • When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
  • You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
  • In this workaround, I think that you can retrieve the edited cells by checking the difference between before and after the edited sheet. But, this is a simple script for explaining this workaround. So, if you use this workaround, please modify the script for your actual situation.

References:

Upvotes: 3

Related Questions