3

I have a call to a toast message within an installable onEdit trigger function that displays a message in the Google Sheets interface whenever an edit is made. The message shows up as expected for users that are logged in to their Google account, but it doesn't show up in the interface when the editor is anonymous.

I have a Sheets file that has anonymous editing enabled ("Anyone with the link"). There is a standalone Google Apps Script project that has installed an installable onEdit trigger. Everything in the function executes successfully for both anonymous and logged in users except for the toast message, which only shows up for logged in users.

The installable onEdit trigger is set up to execute the showMessage function.

Trigger Installed With:

ScriptApp.newTrigger('showMessage').forSpreadsheet('thefileid').onEdit().create();

showMessage Function:

function showMessage(e) {

    var msg = 'Some msg';
    var title = 'Some title';
    var file = e.source;
    var activeSheet = file.getActiveSheet();
    file.toast(msg, title);

    // do other things

}

The toast message appears for logged in users, not anonymous ones. The 'other things' in the function work as expected for everyone. I'm looking for a way to show anonymous users that message (or looking for some way to communicate automated messages to them). The script project is standalone and not container-bound, so I can't use the Ui class to notify them. Container bound scripts are not an option, as this script is substantial in size and gets run on multiple files.

gccree
  • 63
  • 2
  • 7
  • See the [required scopes](https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#toast(String)) in the documentation--have you checked if your anonymous users have this authorization enabled? There's the reference for triggers [here](https://developers.google.com/apps-script/guides/triggers/installable) if you haven't already checked that; something else you might try is *disabling* this trigger and checking that the behavior stops as expected, so as to avoid confusion if your script is actually being triggered by something else (and this doesn't work for anyone). – sinaraheneba Aug 13 '19 at 23:21

2 Answers2

6
  • You want to display a message when the cells of Spreadsheet are edited by anonymous.
  • The Spreadsheet is publicly shared for anonymous users as the editor.

If my understanding is correct, how about this answer? Unfortunately, even when the installed OnEdit event trigger is used, when anonymous users are edited, toast() and Class Ui cannot be used. So as one of several workaround, I would like to propose to use the images. Fortunately, insertImage() can be used for this situation. So I'm using this workaround. Please think of this as just one of several answers.

Before you use this script, please prepare an image for displaying.

Sample script:

Before you use this script, please set the file ID of the image. And please install the OnEdit event trigger for the function of showMessage().

function showMessage(e) {
  var fileId = "###"; // Please set the file ID of the image.

  var sheet = e.source.getActiveSheet();
  var blob = DriveApp.getFileById(fileId).getBlob();
  var image = sheet.insertImage(blob, 2, 3);
  Utilities.sleep(3000);
  image.remove();
}
  • In this sample script, when the cell is edited, the prepared image is displayed and waited for 3 seconds, and then, the image is removed.

Result:

enter image description here

Note:

  • Of course, you can create an image for displaying with the script. But in this case, the process cost will become high. As the result, the time until the image is displayed becomes long. So I proposed to use the image which was created in advance.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks for the idea! This does seem to work for all users anonymous and logged in, though it has gotten stuck on image.remove() when the file has multiple editors. Execution time went from 7s to 25s, and then later to 80s. The 80-second execution didn't stop until I closed the anonymous editor's browser. This seems to be a reasonable workaround to me, though I won't mark it as 'the' answer because I suspect there's something actually wrong with the toast function. – gccree Aug 14 '19 at 15:51
  • @gccree Thank you for replying. I apologize for the inconvenience. Unfortunately, I cannot understand about `when the file has multiple editors`. Can I ask you about the detail information about it? And I deeply apologize that "the toast function" cannot be used for your situation, because of the specification of Google side. The situation that I cannot find other workaround to use "the toast function" is due to my poor skill. Also I have to apologize for this. – Tanaike Aug 14 '19 at 23:33
  • I just mean when there are multiple people simultaneously accessing the document. It's a good workaround though, and I am currently using it. – gccree Aug 15 '19 at 18:24
  • @gccree Thank you for replying. You are using one Spreadsheet by sharing several users. Is my understanding correct? Can I ask you about your current workaround? – Tanaike Aug 15 '19 at 23:31
  • Yeah that's right. I'm currently using your insertImage() workaround. – gccree Aug 16 '19 at 12:47
  • @gccree Thank you for replying. I deeply apologize the specification that "the toast function" cannot be used for your situation and my workaround was not to use "the toast function". – Tanaike Aug 16 '19 at 23:26
0

As you can see in the documentation:

Apps Script requires user authoritaztion to access private data from built-in Google Services or advanced Google services

That means you can share your script with anyone, but they need to log in to use the script.

Jescanellas
  • 2,555
  • 2
  • 9
  • 20