How can I make a Google Sheet script run reliably on Samsung tablets?

荒凉一梦 提交于 2020-01-05 04:22:06

问题


I have two functions in my Google Sheet script which are each triggered by means of a checkbox (since Google Sheets on mobile can't use images as buttons). They work on PC (rather slowly), but on tablets, they tend to fail more often than not, which then also affects PC users.

The script is set up to perform an onEdit check of two checkbox cells. If the checkbox in cell C3 is checked, the AUTOFILL function should run (which displays the A cell value of the last row on the Info sheet plus 1 in cell C4 of the Data Entry sheet, and then clears the checkbox), and if the checkbox in cell C12 is checked, the SUBMIT function should run (which takes the range of data entered on the Data Entry sheet and updates an existing row/adds a new row on the Data sheet with the information from the Data Entry sheet, adding a timestamp if cell C11 on the Data Entry sheet contains the word 'CLEANED', and then clears the checkbox).

I've tried experimenting with various WIFI signal strengths and more powerful tablets, but I am unable to pinpoint the exact culprit here - sometimes this will run, most often the checkbox will just remain checked and nothing happens. Laptops and desktop computers all seem to run, but if a tablet tries to run and fails, the computers will sometimes not run either, until I go into the script itself and manually force once of the functions to run, which seems to reset things and lets the computers work again.

Is it because of the processing required in order to run this code? I've tried to optimize it as much as possible, but is there something else that I might change here which would make this work, every time?

Here is the example sheet, and here is the script:

function onEdit(e) {
   if (e.range.getSheet().getName() != "Data Entry") {
       return
   }

  var isAutofill = SpreadsheetApp.getActiveSheet().getRange("C3").getValue();
  var isSubmit = SpreadsheetApp.getActiveSheet().getRange("C12").getValue();

    if (isAutofill && isSubmit) {
    Browser.msgBox("You cannot autofill and submit data at the same time!");
    SpreadsheetApp.getActiveSheet().getRange("C3").setValue(false);
    SpreadsheetApp.getActiveSheet().getRange("C12").setValue(false);
  } else if (isAutofill) {
    AUTOFILL();
    SpreadsheetApp.getActiveSheet().getRange("C3").setValue(false);
  } else if (isSubmit) {
    SUBMIT();
    SpreadsheetApp.getActiveSheet().getRange("C12").setValue(false);
  }
}


function AUTOFILL() {
  var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Info');
  var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data Entry');
  var valueOfData = sheet1.getRange(sheet1.getLastRow(), 1).getValue();
  sheet2.getRange('C4').setValue(valueOfData + 1);
} 


function SUBMIT() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Data Entry");
var dataSheet = ss.getSheetByName("Info");

var values = formSS.getRange("C4:C11").getValues().reduce(function(a, b) {
    return a.concat(b)
});
var partNum = values[0];
var row;
dataSheet.getDataRange().getValues().forEach(function(r, i) {
    if (r[0] === partNum) {
        row = i + 1
    }
})
row = row ? row : dataSheet.getLastRow() + 1;
var data = dataSheet.getRange(row, 1, 1, 8).getValues()[0].map(function (el, ind){
  return el = values[ind] ? values[ind] : el;
  })

var statusValue = formSS.getRange("C11").getValue();

if (statusValue != 'CLEANED') {
dataSheet.getRange(row, 1, 1, 8).setValues([data]);
}

if (statusValue == 'CLEANED') {
var now = [new Date()];
var newData =  data.concat(now)
dataSheet.getRange(row, 1, 1, 9).setValues([newData]);
}

formSS.getRange("C4:C11").clearContent()
}

回答1:


I made a few changes. Take a look. Hopefully you can use them to speed up the function.

function onEdit(e) {
  var sh=e.range.getSheet();
  if (sh.getName() != "Data Entry") {return;}
  var rgC3=SpreadsheetApp.getActiveSheet().getRange("C3");
  var rgC12=SpreadsheetApp.getActiveSheet().getRange("C12");
  var isAutofill = rgC3.getValue();
  var isSubmit = rgC12.getValue();

  if (isAutofill && isSubmit) {
    e.source.toast("You cannot autofill and submit data at the same time!");
    rgC3.setValue(false);
    rgC12.setValue(false);
  } else if (isAutofill) {
    AUTOFILL(e.source);
    rgC3.setValue(false);
  } else if (isSubmit) {
    SUBMIT(e.source);
    rgC12.setValue(false);
  }
}

function AUTOFILL(ss) {
  var sheet1 = ss.getSheetByName('Info');
  var sheet2 = ss.getSheetByName('Data Entry');
  var valueOfData = sheet1.getRange(sheet1.getLastRow(), 1).getValue();
  sheet2.getRange('C4').setValue(valueOfData + 1);
} 

function SUBMIT(ss) {
var formSS=ss.getSheetByName("Data Entry");
var dataSheet=ss.getSheetByName("Info");
var values=formSS.getRange("C4:C11").getValues().reduce(function(a, b) {return a.concat(b)});
var partNum = values[0];
var row;
var data=dataSheet.getDataRange().getValues()
for(var i=0;i<data.length;i++) {
  if(data[i][0]==partNum) {
    row=i+1;
    break;
  }
}
row = row ? row : dataSheet.getLastRow() + 1;
var data = dataSheet.getRange(row, 1, 1, 8).getValues()[0].map(function (el, ind){return el = values[ind] ? values[ind] : el;})
var statusValue = formSS.getRange("C11").getValue();
if (statusValue != 'CLEANED') {dataSheet.getRange(row, 1, 1, 8).setValues([data]);}
if (statusValue == 'CLEANED') {var now = [new Date()];var newData=data.concat(now);dataSheet.getRange(row, 1, 1, 9).setValues([newData]);}
formSS.getRange("C4:C11").clearContent();
}


来源:https://stackoverflow.com/questions/58343512/how-can-i-make-a-google-sheet-script-run-reliably-on-samsung-tablets

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