Create Triggers from Button in Ui

73 views
Skip to first unread message

sben...@bbchs.org

unread,
Aug 17, 2021, 9:56:18 PM8/17/21
to Google Apps Script Community
I have this as the the first part of my function.  Works fine when ran from editor, will not work when ran from a button in the Ui.  
Anybody have any fixes.  I have people making copies of this file then for themselves but they need the trigger created.

  let triggers = ScriptApp.getScriptTriggers().length
  if(triggers==0){
    ScriptApp.newTrigger('updateTimes').timeBased().atHour(7).everyDays(1).create()
    }

Alex

unread,
Aug 18, 2021, 12:55:40 AM8/18/21
to Google Apps Script Community
Hi!

Can you share a sample?

The best way for this is to create an user menu (not an image button).

ธีระศักดิ์ คนตรง

unread,
Aug 18, 2021, 4:49:59 AM8/18/21
to Google Apps Script Community
//Try it;
function createTrigger() {
  let triggers = ScriptApp.getScriptTriggers().length
  if (triggers == 0) {
    ScriptApp.newTrigger('updateTimes').timeBased().atHour(7).everyDays(1).create()
  }
}

function setTrigger(){
  var ui = SpreadsheetApp.getUi();
  var res = ui.alert('create Trigger?',ui.ButtonSet.YES_NO)
  if(res === ui.Button.YES){
    createTrigger();
  }
}

ในวันที่ วันพุธที่ 18 สิงหาคม ค.ศ. 2021 เวลา 8 นาฬิกา 56 นาที 18 วินาที UTC+7 sben...@bbchs.org เขียนว่า:

Bennett, Scott

unread,
Aug 18, 2021, 7:55:21 AM8/18/21
to google-apps-sc...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/ea2c10c3-967d-417f-9ccf-ae1d8a94bd6cn%40googlegroups.com.


--
That did the trick, thanks.  

Bennett, Scott

unread,
Aug 18, 2021, 10:27:33 AM8/18/21
to google-apps-sc...@googlegroups.com
It worked on mine but did not work when a copy was made.
--
Scott Bennett
Data and Assessment Coordinator/Math Teacher
Bradley-Bourbonnais Community High School

Bennett, Scott

unread,
Aug 18, 2021, 10:32:25 AM8/18/21
to google-apps-sc...@googlegroups.com
Yes, It is being done from a menu.  I added menu buttons to the Ui.

function onOpen() {

  SpreadsheetApp.getUi().createMenu('Advanced Menu').addItem('Create Teacher Sheets','createTeacherSheets').addSeparator().addItem('Update Times','updateTimes').addItem('Show/Hide Info Sheet','showHideSheet').addSeparator().addItem('Reset Sheet','resetSheet').addToUi();
  
}

function setTrigger(){
  var ui = SpreadsheetApp.getUi();
  var res = ui.alert('create Trigger?',ui.ButtonSet.YES_NO)
  if(res === ui.Button.YES){
    ScriptApp.newTrigger('updateTimes').timeBased().atHour(7).everyDays(1).create()
  }
}
function showHideSheet(){
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let infoSheet = ss.getSheetByName('Information');
  let status = infoSheet.isSheetHidden();
  if(status == true){
    infoSheet.showSheet()
  }
  else if(status ==false){
    infoSheet.hideSheet();
  }
}

Then I have the setTrigger running on a different function if the condition is met that there are no triggers.

Alex

unread,
Aug 18, 2021, 3:08:45 PM8/18/21
to Google Apps Script Community
Well done!
Reply all
Reply to author
Forward
0 new messages