How can I get a macro to automatically trigger when a cell reaches a certain value in a Google Sheet?

▼魔方 西西 提交于 2020-04-16 09:15:10

问题


What I need is for a macro I've recorded called SwitchHotSeat to trigger when the value of cell F3 goes above £1,000,000.00 0r 1000000 or the LEN of cell F3 goes over that length.

I can find guides for doing this in Excel, but not for Google Sheets. below is just the code for my macro.

function SwitchHotSeat() {
  var spreadsheet = SpreadsheetApp.getActive();
      spreadsheet.getRange('3:3').activate();

 spreadsheet.getActiveSheet().deleteRows(spreadsheet.getActiveRange().getRow(), spreadsheet.getActiveRange().getNumRows());
  spreadsheet.getActiveRangeList().setBackground('#ff0000')
  .setFontColor('#ffffff');
};

The macro works fine. I just need a way of triggering it when that cell goes over the 1000000.


回答1:


You could use simple or installable triggers. See

  • https://developers.google.com/apps-script/guides/triggers
  • https://developers.google.com/apps-script/guides/triggers/installable

Example of using onEdit simple trigger

function onEdit(e){
  if(/* add here your conditions */) {
     SwitchHotSeat();
  }
}

NOTE: on edit and on change triggers are triggered only when an user edit the spreadsheet.




回答2:


Just in case it helps anyone else with a similar issue, I discovered (by logging) that it was returning an object, rather than a number before, so the following code worked fine in the end.

function onFormSubmit(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
   var millionaire24 = ss.getSheetByName('Millionaire24.');
    var cellValue = millionaire24.getRange(3,6,1,1).getValues();
  Number(cellValue)
if(cellValue[0] >= 1000000)switchHotSeat();


}


来源:https://stackoverflow.com/questions/54065872/how-can-i-get-a-macro-to-automatically-trigger-when-a-cell-reaches-a-certain-val

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!