google sheets - How do I associate check-box button with an App Script code? - Stack Overflow

I have a function written with Google App Script. I want to execute the code by clicking on a button on

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 0
Add a comment  | 

1 Answer 1

Reset to default 1

You can use the assign script to a drawing

To execute a code using a button, you can follow these steps:

  1. Go to Insert tab
  2. Then Drawing
  3. From then you can create your own button depending on your design
  4. Save and close.
  5. Right click on the drawing, then click the menu option
  6. 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信