I have a function written with Google App Script. I want to execute the code by clicking on a button on Google Sheet. It may be a simple button and I associate it with the App Script code.
I have written the code and it is working properly when I run it on the App Script page by clicking on the "Run" button, it executes correctly like what I expect.
I have a function written with Google App Script. I want to execute the code by clicking on a button on Google Sheet. It may be a simple button and I associate it with the App Script code.
I have written the code and it is working properly when I run it on the App Script page by clicking on the "Run" button, it executes correctly like what I expect.
Share Improve this question edited Nov 20, 2024 at 13:00 Wicket 38.5k9 gold badges78 silver badges193 bronze badges asked Nov 20, 2024 at 11:51 yalçın karakoçyalçın karakoç 11 01 Answer
Reset to default 1You can use the assign script
to a drawing
To execute a code using a button, you can follow these steps:
- Go to
Insert
tab - Then
Drawing
- From then you can create your own button depending on your design
- Save and close.
- Right click on the drawing, then click the menu option
Assign script
7. Input your function name.
Once you have done these steps you can now just click on the button and the function will execute automatically.
Sample function execution by clicking the button.
Alternatively, you can use OnEdit
or OnSelectionChange
.
Here is a sample of OnEdit()
trigger that when the checkbox is checked
or set to TRUE
the script will call the function that deletes a row.
Sample code:
function onEdit(e) {
var range = e.range;
var sheet = range.getSheet();
if (sheet.getName() == 'Sheet1' && range.getColumn() == 2) {
var isChecked = range.getValue();
if (isChecked) {
var row = range.getRow();
deleteRowValues(row);
}
}
}
function deleteRowValues(row) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
sheet.deleteRow(row);
}
Sample Output:
*This code is only intended to show how OnEdit works.*
Please change the trigger, conditions, names, and functions of your code depending on what's best for your needs.
To run a trigger, you need to run the code at least once. however, please expect an error as this is normal for a trigger. If you are using an event object and you in case that you need to debug your code using the "console.log" or "Logger.log", on a trigger you can use SpreadsheetApp.getActiveSpreadsheet().toast();
, this will help you show the changes and how the trigger interact with your sheet.
Please check the documentation below as there are a bunch of triggers you can choose from.
Reference: Simple Trigger
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742360059a4429213.html
评论列表(0条)