1

Hopefully I am in the right place. I am working on building a maintenance program and I am having some trouble. What I have is a work order form created in sheets, I have a script wrote that will save a copy of the template under a new name (tied to a cell value in the sheet), a folder on drive. I also have this script scheduling the work order on a google calendar. This code is below.

function Create() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get spreadsheet
  var id = ss.getId(); // Get spreadsheet ID
  var sstocopy = DriveApp.getFileById(id); //Get spreadsheet with DriveApp
  var sheet = ss.getActiveSheet(); // Grab the sheet in order to find the name of the new spreadsheet 
  to be created
  var sheet_name = sheet.getRange("C4").getValue(); // Get the value, in this case: Project Urn
  var folder = DriveApp.getFolderById("1D9gWUuHTPbWkzpSOp5vQdg-K8N6kUGo6"); // Get the folder where 
  the sheet needs to be placed.
  sstocopy.makeCopy(sheet_name,folder); // Make the copy of the sheet in that folder.

  var eventCal = 
  CalendarApp.getCalendarById("c_4um01s3eqvq7d5kqi0ga4i1098@group.calendar.google.com") ;
   var eventname = sheet_name;
   var eventstart = sheet.getRange("E3").getValue();
 var eventstop = sheet.getRange("E4").getValue();
  eventCal.createAllDayEvent(eventname, eventstart, eventstop);
  } 

Now this code works great, I originally had a clickable button on the sheet to run this code, and that worked great from the computer. The sticky part is that I want to be able to run this off the mobile app from a Ipad. So I have been trying to set up a drop down menu in order to run the script. My sheets file has several pages/tabs, and I think this is what is giving me issues. I am assuming that I need to call the right sheet but I can't seem to get it to work.

The first code I tried was this (except my drop down is in F3)

function onEdit(e) {
  if (e.range.getA1Notation() == 'A1') {
    if (/^\w+$/.test(e.value)) {        
      eval(e.value)();
      e.range.clear();
    }
  }
}
    

But I get an undefined error for e.range.getA1Notation. I've tried some other things but I don't really know what I'm doing, as my understanding is pretty basic, and its been a long time since I really did anything like this. Any help would be appreciated.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Welcome to [so]. By droop-down menu you mean Google Sheets cell data validation, right? By the other hand running any function from the script editor that requires an argument it will throw the undefined error ... – Rubén Jun 30 '20 at 19:11
  • Does this answer your question? [How can I test a trigger function in GAS?](https://stackoverflow.com/questions/16089041/how-can-i-test-a-trigger-function-in-gas) – Rubén Jun 30 '20 at 19:15
  • 1
    Yes I meant drop down menu as in a data validation list of items in a cell. – Matt Burtis Jun 30 '20 at 20:26

1 Answers1

3

Here's one way you can use checkboxes as button's to run functions. I used an installable onEdit(e) trigger for this purpose so that it could perform functions that require permission..

function onMyEdit(e) {
  e.source.toast('Entry');
  console.log(JSON.stringify(e));//use this to see all of the features in the event object
  const sh=e.range.getSheet(); 
  if(sh.getName()=="Your Sheet Name" && e.range.columnStart==1 && e.value=="TRUE" ) {
    e.range.setValue('FALSE');
    switch(e.range.rowStart) {
      case 1:
        e.source.toast('function 1');
        break;
      case 2:
        e.source.toast('function 2');
    }
  }
}

I don't have any function attached the idea is that where I have e.source.toast('function 1'); you would replace that whole line with a function call. And this is setup to run off of only one page but that could be modified if desired.

Animation:

enter image description here

As you can see it can be setup so that the checkbox is reset every time and instead of the toast showing up your selected function would show up. You can always put labels next to the checkboxes for the function names.

And this example uses datavalidation to create a dropdown function selection process. It's important to keep in mind that the onEdit() function has to complete in 30 seconds.

function onMyEdit(e) {
  e.source.toast('Entry');
  console.log(JSON.stringify(e));
  const sh=e.range.getSheet(); 
  if(sh.getName()=="Your Sheet Name" && e.range.columnStart==1 && e.value=="TRUE" ) {
    e.range.setValue('FALSE');
    switch(e.range.rowStart) {
      case 1:
        e.source.toast('function 1');
        break;
      case 2:
        e.source.toast('function 2');
    }
  }
  if(sh.getName()=="Your Sheet Name" && e.range.columnStart==2 && e.range.rowStart==1 && e.value) {
    switch(e.value) {
      case '1':
        e.source.toast('function 1');
        break;
      case '2':
        e.source.toast('function 2');
    }
    e.range.setValue('');
  }
}

Animation:

enter image description here

Event Object

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Are these just portions of code? I copied and pasted into a new sheet just to try out and try to learn how it works but nothing seems to happen? Do I need to activate something because of the installed trigger? I didn't even know what a toast was untill a sec ago when i searched it. Just tried changing the onMyEdit to just onEdit and it does kinda work but just entry shows up in the message. – Matt Burtis Jun 30 '20 at 22:33
  • Well I recommed that you just take one of them. Let's take the bottom one since it contains everything. – Cooper Jun 30 '20 at 22:39
  • And for now let's just limit it to one page and after you figure out what's going on you will see how to extend it. You need to write the sheet name into where it says "Your Sheet Name" – Cooper Jun 30 '20 at 22:40
  • And you need to add a couple of checkboxes at A1 and A2 and a datavalidation at B1 with two options 1 and 2 – Cooper Jun 30 '20 at 22:42
  • Once you get all of that done you also need to create an installable trigger for the function onMyEdit(e) and you can do that through the Edit/Current Project Triggers menu. Once you're all done with that you should see it start to run when you click on the check boxes or the dropdown. Then we can discuss how to add the functions – Cooper Jun 30 '20 at 22:43
  • First thanks for helping me through this. I have the bottom code working fine in a blank spreadsheet. As long as I leave the checkboxes in A1,A2 and the Dropdown in B2. In my actual sheet I want the options in a different cell, so I attempted to move them. I now have the drop down working in cell G3 by changing e.range.columnStart==7 && e.range.rowStart==3 I tried performing a similar change on the checkboxes by adding the e.range.rowStart==3 bit and changing the other portion to e.range.columnStart==6. Checkboxes are in cells F3, F4. But I just get entry instead of Function 1 or 2 – Matt Burtis Jul 01 '20 at 03:46
  • I do have it running my create function now via a drop down menu in my actual form on cell F3. Will have to play with getting the checkboxes to work, but making progress. This sort of programming is quite the stretch for this mechanical engineer turned electrician/mechanic so appreciate the help and guidance, while not just writing the code for me. – Matt Burtis Jul 01 '20 at 04:46
  • Sorry I left you hanging there but I had to regenerate. Where are we at. Do you need any help? – Cooper Jul 01 '20 at 17:21
  • You really want to take as much advantage of the event object as possible because you'll want to limit the number of function calls as much as possible since onedit triggered functions have to complete in 30 seconds. – Cooper Jul 01 '20 at 17:23
  • I have the dropdown running the script now. I still don't completely understand the (e) stuff and I have everything broke into small function which I call which probably isn't efficient but it is functional, so thank you – Matt Burtis Jul 01 '20 at 20:32
  • onedit(e) functions are triggered by a simple trigger that occurs when a human edits a cell in a spreadsheet. The same is true for installable triggers but they can be used with functions that can perform operations that require authentication. Both triggers supply the function with an event object and it's standard practice to use the e to hold the event object. [event object](https://developers.google.com/apps-script/guides/triggers/events#edit) – Cooper Jul 01 '20 at 20:41