sidlo
sidlo

Reputation: 183

Google Sheets - How to run a script from iOS app?

I use a script in my SpreadSheet to add row below the active one and copy there content from active row. On PC I can trigger the script via image/drawing and it works nicely. But I cannot figure out how to make it work in iOS app where trigger from image doesn't work and where copying content from one to another is nightmare. I tried onEdit and isChecked for each line but it would give me new row on every edit. Could you help, please?

function addRow() {
var sheet = SpreadsheetApp.getActiveSheet(),
    row = sheet.getActiveCell()
        .getRow(),
    rangeToCopy = sheet.getRange(row, 1, 1, 20);
sheet.insertRowAfter(row);
rangeToCopy.copyTo(sheet.getRange(row + 1, 1));
}

https://docs.google.com/spreadsheets/d/12WErwXRn6yPvG8FuR-DwltaSC-Fuk7aTB2NrJWMKFQ4/edit?usp=sharing

Upvotes: 4

Views: 8240

Answers (2)

Jeremy Irons
Jeremy Irons

Reputation: 534

Check-boxes will work great on the iOS interface, I have actually used this for a major restaurant with high-volume delivery orders (checking off "ready" orders and auto-texting drivers that their order is ready). The only change we need to make to your spreadsheet is to add a checkbox for each individual row. enter image description here

The trick is to use an onEdit trigger, by renaming your function "onEdit"

Here is the script:

 function onEdit(e) {
      //IF the cell that was edited was in column 1 and therefore a checkbox AND if the cell edited was checked (not unchecked):
      if (e.range.columnStart === 1 && e.range.getValue() === true) {
        var sheet = SpreadsheetApp.getActiveSheet(),
            row = sheet.getActiveCell()
            .getRow(),
            rangeToCopy = sheet.getRange(row, 1, 1, 20);
        sheet.insertRowAfter(row);
        rangeToCopy.copyTo(sheet.getRange(row + 1, 1));
        //Reset checked boxes
        sheet.getRange(row,1,2,1).setValue(false);
      }
    }

NOTE: if you check a bunch of boxes quickly in succession, google scripts might miss some. This is fine because, they will stay checked and you will be able to see the ones that didn't run. Simply un-check and check them again to run the script as normal.

Upvotes: 4

Cooper
Cooper

Reputation: 64140

You could run your function with something like this:

function onEdit(e) {
  const sh=e.range.getSheet();
  if(sh.getName()=='Your sheet name' && e.range.columnStart=='column you choose' && e.value=='TRUE') {
    e.range.setValue('FALSE');//resets the checkbox
    const row=e.range.rowStart;
    yourfunction(row);
  }
}

I would use one column of checkboxes and which ever one you check it will run your function and pass the active row to it. Or you could pass e to it and get active row with e.range.rowStart.

Upvotes: 2

Related Questions