1
  1. Project_1 is a container-bound script. A container is a readable spreadsheet (Template).

Code_1:

function doPost(e) {
  return HtmlService.createHtmlOutput(JSON.stringify(e));
}

The user makes a copy of the Template, deploys the script (Project_1) as a webapp with permissions: "Execute as: Me, Who has access: Anyone". The user is the owner of Project_1.

  1. Project_2 is a script deployed as an add-on. The user from point 1 is not the owner of Project_2.

Code_2:

function sendPost() {
  var sheetURL = SpreadsheetApp.getActiveSpreadsheet().getUrl();

  var webAppUrl = "https://script.google.com/macros/s/###/exec"; // 7: Part_1 - WebApp: Tester

  // var auth = ScriptApp.getOAuthToken();
  // var header = { 'Authorization': 'Bearer ' + auth };
  var payload = { scriptName: 'updateData', sheetURL: 'sheetURL' };
  var options = {
    method: 'post',
    // headers: header,
    muteHttpExceptions: true,
    payload: payload
  };

  var resp = UrlFetchApp.fetch(webAppUrl, options);
  var respTxt = resp.getContentText();
  console.log('resp: ' + respTxt);
}

function doPost(ev) {
  var respWebapp = func(ev);
}

The user installs an add-on (Project_2).

  1. The flow in the direction of addon -> webapp is fine: when sendPost() starts, it sends a request to the webapp and receives a response with the necessary data_1 in response.

The flow in the direction of "someone on the web" -> webapp also flows well: when requesting a webapp_url receives the transferred data_2.

  1. I am trying to transfer data_2 to an addon. I read a lot about scripts.run, but it seems that this option is not applicable in such a situation. There is also nowhere to add an eventListener.

  2. I would not want to deploy webapp from my account, so as not to spend my quota for simultaneous executions (<= 30). Also I would not like to do a sidebar, etc. in the spreadsheet and try to screw eventListener to html. I assume that with this approach, the listener (if it is possible to add it there at all) will be active only when ui is active (the spreadsheet is open and the sidebar is active). Data can come at any time of the day and must be immediately transferred to the addon.

Added: I feel like I'm stumped. Therefore I reaches out to the community in the hope that someone would suggest a workaround or a new strategy for this initial data. By initial data I mean provide the opportunity for more than 30 users to exchange messages in both directions Spreadsheet <--> External service (for example, Telegram) and at the same time not fall under the limit of 30 simultaneous script executions.

Added_2: I'm going to assign a bounty, so I'm transferring here from the comments what is missing in the post and updating the question itself.

I rejected the option with immediate entry into the sheet, because this will cause constant calls to the spreadsheet and slow down the performance of the system as a whole.

I am aware of the existence of Google cloud functions and Google compute engine, but would like to stay within the free quotas.

QUESTION: How to transfer data from webapp to addon and execute func () or which workaround to use to achieve the goals?

26
  • 1
    @TheMaster "The user ... deploys the script (Project_1) as a webapp with permissions:" Execute as: Me, Who has access: Anyone "". The user will spend not mine but his quota. Oct 31, 2021 at 21:10
  • 1
    Google Apps Script Workspace Editor haven't incoming/outgoing end-points to get data from external code, so you will have to rethink your solution.
    – Rubén
    Oct 31, 2021 at 22:21
  • 2
    Please update the question accordingly. P.S. One possibility among many is to use a spreadsheet as a database to be used as data store by "Code_1" and "Code_2".
    – Rubén
    Oct 31, 2021 at 22:48
  • 1
    You could look into external resources like firebase or a database. Then you could poll from webapp#2. Of course if you're willing to live with se of 30, you could always deploy webapp#2 from your addon. Alternatively, with a Google account, but a service. Maybe make the service run as the user?
    – TheMaster
    Nov 1, 2021 at 6:52
  • 1
    It's highly unlikely for 50 users to send a command at exactly the same time. As long as your webapp is fast and executes under 30s or so, I don't think this will be a problem with 50 users. If you're worried, look into professional servers/domains- like Google cloud functions, Google compute engine.
    – TheMaster
    Nov 1, 2021 at 20:29

3 Answers 3

1

Here is a list of your requirements:

  • Trigger add-on code to run from some external request, not using the add-on user interface or time based trigger.
  • Code runs from the user's account, using their quota
  • Run the add-on code regardless of whether the user is using the add-on or not. For example, their Google Sheet is closed, and the user may even be signed out.

I only know of one way to do that, and it's with a Sheet's add-on by triggering the "On Change" event by setting a value in a Sheet cell using the Sheets API. The Sheets API must use a special option to set the value "As the User."

The special setting is:

valueInputOption=USER_ENTERED

That option will trigger the "On Change" event even if the Sheet is closed. Obviously the script making the request needs authorization from the user to set a value in a cell of the Sheet. If the script sending the request is outside of the user's account then you'd need to use OAuth. The add-on would need to install an "On Change" trigger for the Sheet and the function that the trigger is bound to would need to determine whether the change was from the special cell designated for this special functionality.

If the request to set a value in the users Sheet is from outside of that users Google account, then the user of the Sheet would need to somehow authorize the OAuth credentials for the Sheets API to make a change to the Sheet.

Depending upon the programming language being used with the Google Sheets API, there may be a Sheets API Library specifically for that language. You can also use the Sheets REST API.

There is an example here on StackOverflow of using the Sheets REST API from Apps Script, but if the external request is from some code that isn't Apps Script, it won't be exactly the same.

12
  • Thanks Alan, this is a very good option. More precisely, he is the only one today. Perhaps in the future it will be possible to pass data or at least a tag to the add-in without tricks. I checked it works in an container-bound script which combines web app code and add-on code. To check exactly as in the title of the post, I can not. Unfortunately Installable triggers aren't supported when testing. I hope when the add-in passes the test, the effectiveness of the method will be confirmed. Apr 27, 2022 at 0:22
  • 1
    Concerning the Sheets API. You need to associate the stand alone Apps Script file with a Google Cloud Project. That must be done in order to publish the add-on, but also to enable API's. So, you need to do more than just set the dependency in the manifest file. The user who installs the add-on doesn't need to enable any API. Google doesn't explain how that works, but because YOU enabled the API in YOUR GCP project, it somehow works for the user.
    – Alan Wells
    Apr 27, 2022 at 18:17
  • 1
    I've had serious problems creating the YouTube video for the add-on review. I was able to overcome those problems, but it's not easy. When I recorded the video, I did some of the recording from my "dev" version which uses the bound Apps Script file, and then recorded another part of the video from a different Google account. How the people at Google test the add-on, I'm not sure. They should have a way of testing it that can install the triggers. When the YouTube video shows the authorization dialog box, you need to be using the Apps Script file from which the real add-on will be published.
    – Alan Wells
    Apr 28, 2022 at 1:03
  • 1
    The reason you need to use the Apps Script file from which the add-on will be published for the authorization dialog, is that there is an ID number in the address bar at the top, that the reviewers must verify is coming from your account. I'm guessing that this requirement is for legal purposes so that Google can cover themselves legally. But other than showing that ID number in the authorization dialog, the rest of the video content could come from another Apps Script file. Plus I didn't want to show the email address of my development account in the video, so I used a different account.
    – Alan Wells
    Apr 28, 2022 at 1:12
  • 1
    If the error was caused by something other than the restriction of "Test as addon" not allowing installation of triggers, it would have stated something else. So, by the logic of excluding what is not probable, I believe the error is caused by that restriction. I never use the "Test as add-on" feature from the code editor. Without triggers, the add-on is useless, and we need to test the triggers. If your add-on has a lot of code, then copying the "dev" version code over to your "production" version could be a lot of work. So I have a special program to do that.
    – Alan Wells
    Apr 29, 2022 at 13:57
1

I understand that the solutions proposed in the comments, by others and myself, can't work in your scenario because it can't stand an average delay of 30 seconds. In that case I strongly advise you to set up a Cloud project that can be used as an instant server, as opposed to triggers/apps/etc.

2
  • It looks like it. First, the palisade of quotas and limitations will not allow using a trigger that will request external data once every 1-2 seconds. Secondly, the connection with the addon is one-way. It is not possible to transfer data inside. If only it was possible to send the flag to the addon that new data has arrived. Then it was possible to request data from an external source from within the addon. Nov 17, 2021 at 0:09
  • There was a hope that the collective intelligence would suggest something, but it did not come true. Therefore, only cloud-based functions remain. But as I wrote in the post, I would like to stay within the free quotas. And the point is not that this is a paid service. The point is, I don't understand anything about it. And there are no good manuals. Nov 17, 2021 at 0:13
0

In "Code_1" and "Code_2" use a shared data store. In other words, instead of directly passing the data from "Code_1" to "Code_2", make that Code_1 write to the datastore and "Code_2" read from it.

One possibility among many is to use a spreadsheet as a database. In this case you might use on change triggers to do some action when the spreadsheet is changed by one of the "Code_1" scripts and/or use time-driven triggers to do some action with certain frequency or at certain datetime.

10
  • Yes, now Project_1 and Project_2 share PropertyService. But I fear that there will be a limitation for the addon (Project_2) "Properties read / write 50,000 / day". And I couldn't think of how to get Project_2 to respond to a property change. That's why I wrote about EventListener and scripts.run. It didn't occur to me to use onChange, because I thought it only fires when changed by the user and not by the script. Nov 1, 2021 at 2:27
  • In the first version of the project, the scripts Project_1 and Project_2 is combined. When testing, it in many parts of the code, outputs were installed into a custom logger with a record on a sheet. At the same time, some users noted lags between sending a message to the bot and its response message up to 10 seconds. After I removed all logging the delays do not exceed 1 second. Therefore, I rejected the option with immediate entry into the sheet, because this will cause constant accesses to the spreadsheet and slow down the performance of the system as a whole. Nov 1, 2021 at 2:30
  • Similar concerns arise with the use of a timing trigger. Perhaps I can put a minimum delay (1 second? I didn't see the minimum number anywhere) between trigger firing. I have never used a trigger with such a timing. Therefore I do not know how correctly the project as a whole will work with such a trigger. And how will Google servers feel if thousands of scripts are hammered into them every second =) In addition, there is a high probability of running into the "Triggers total runtime 90 min / day" limit. Nov 1, 2021 at 2:31
  • 2
    Maybe you should go several steps back and think about what lead to make a solution that require the users to publish a web-app instead of asking them to use the same web app.
    – Rubén
    Nov 1, 2021 at 3:42
  • 1
    Besides what was already said by Tanaike and Master I think that the last statement does a big jump by mentioning add-on as all the previos question development was only about web-app. May suggestion is to center on understanding how a the web-app deployment options works.
    – Rubén
    Nov 1, 2021 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.