Hi Friends,
Is there a simple way to have an Action button on a Detail View of an AppSheet app that can trigger a script which works perfectly on the underlying Google Sheet?
Iโve created the script, then created a button which is placed on one of the sheets. Iโve then assigned the Script to the button, and clicking on the button from the sheet works as expected. However, I need either the Script or the Button be triggered from an Action Button from the App. How do I do this, simply?
Thanks for your help!
Parag
There is a post around here somewhere, but the script trigger must be onchange if I remember correctly. Maybe you can adjust your script.
@Parag
You need to publish that script as a WebApp and then use its URL as a webhook endpoint in AppSheetโs Webhook Workflow.
Hi Levent,
Thanks for your prompt response!
While it did answer my question to a point, it has raised some more!
Before I get into the details, please know that Iโm VERY new to all this-about a month: Google Sheets, AppSheet or any kind of coding!
So, I followed your suggestion and published my Script as WebApp and used its URL as a Webhook in the Workflow of the ApSheet. So far, so good
But it doesnโt work!
Upon testing, I got the following error message:
Failed: Action not performed because 1 errors are present. Error: Failed to parse JSON due to Unexpected character encountered while parsing value: h. Path โโ, line 0, position 0โฆ
My Script is very simple:
function addRecord() {
var ss=SpreadsheetApp.getActive();
var sh1=ss.getSheetByName(โRandomโ);
var sh2=ss.getSheetByName(โAddโ);
var Range=sh1.getRange(โD2:D2โ).getValues();
sh2.appendRow(Range[0]);
}
It simply adds a new record, in a new Tableโs appended row, from a list of random names from another Table. Iโve assigned this Script to a button on the Google Sheet, and it works perfectly, as expected, if Iโm clicking the button from the Sheet. It does not have, nor does it need any other triggers such as onOpen, onChange, etc. It is a manual โclickโ on the button, and needs to remain a manual click.
Now on the AppSheet app that Iโve created, the app pulls the name (and the associated information such as images, etc.) from the newly appended row in Google Sheet and presents it like a slideshow to the user of the app, in a completely random manner. However, if the user of the app is able to click/touch a button on the APP, which in turn triggers the Script on the Google Sheet, then they would not need to have access to the Sheet! I need that to keep the sheet safe! As a workaround, Iโve placed an Action button on the App, which forwards one โslideโ at a time upon clicking the App button in a Detail View. (But for this I need to manually โadvanceโ all the slides on the Sheet (which the user is not aware of), but then it is not truly dynamic or random, for that matter! And I always need to stay involved!!
After I got the above error, I researched the publishing of the WebApp. There seem to be two requirements for that:
"A script can be published as a web app if it meets these requirements:
doGet(e)
or doPost(e)
function.HtmlOutput
object or a Content service TextOutput
object."As you can see, I donโt have either doGet(e) or doPost(e) in my Script, and I donโt even know what the โobjectsโ are in the second requirement!
So these questions:
Iโve also CCd Praveen so that you folks can think of how to make this whole experience truly no-code, to encourage people like me!
Many thanks again!
Warm regards,
Parag Raval
@Parag
Your code shall be like this. After saving the changes, re-publish the WebApp, from version dropdown choose new and press publish.
function doPost(e) {
try {
var ss=SpreadsheetApp.openById("Your_Sheet_ID_Here");
var sh1=ss.getSheetByName(โRandomโ);
var sh2=ss.getSheetByName(โAddโ);
var Range=sh1.getRange(โD2:D2โ).getValues();
sh2.appendRow(Range[0]);
return ContentService // return json success results
.createTextOutput(
JSON.stringify({"result":"success",
"data": JSON.stringify(data) }))
.setMimeType(ContentService.MimeType.JSON);
} catch (e) {
MailApp.sendEmail("your_email_address_here", "Webhook Error Occured!",
"\r\nMessage: " + e.message
+ "\r\nFile: " + e.fileName
+ "\r\nLine: " + e.lineNumber);
e = (typeof e === 'string') ? new Error(e) : e;
Logger.severe('%s: %s (line %s, file "%s"). Stack: "%s" . While processing %s.',e.name||'',
e.message||'', e.lineNumber||'', e.fileName||'', e.stack||'', processingMessage||'');
throw e;
}
}
Hi Levent,
Thanks for all the info and the code for the script.
Iโll check these out and try to implement them as well.
Iโll keep you posted.
Many thanks, again!
Parag
@Parag
Provided you only want to copy values from one sheet to another and create new record please check this where you donโt need a script at all:
Option#1: Using AppSheet Behavior Action
Hi Levent,
Pursuant to your suggestions, I was successfully able to create buttons on my app that trigger the app scripts on the underlying Google Sheet.
My follow up questions are as follows:
Many thanks for your great help!
Warm regards,
Parag
Can you please expand the idea?
onEdit(e) installable trigger only works if you are interacting with the spreadsheet. However, onChange(e) installable trigger works with AppSheet.
Regarding what you want to accomplish, you can use Scheduled Reports for that purpose. If you can expand the idea, I may advise in detail.
User | Count |
---|---|
15 | |
15 | |
8 | |
7 | |
4 |