umer farooq
umer farooq

Reputation: 1

how to run app scripts on mobile, there is one script working another not working

I have to use a mobile while I am traveling for managing google sheets. I have two functions which I have assigned to buttons it is working. however, it doesn't work in mobile version. for this, I have found a why by filling a checkbox in a cell with onEdit function. but one function is working while another is not working even in desktop version when I click a checkbox

function onEdit(e){
const rg = e.range;
if(rg.getA1Notation() === "C30" && rg.isChecked() && rg.getSheet().getName() === "pad no 1"){
loopThroughQuantity();  //loopThroughQuantity is a function name
rg.uncheck();
}
}

now I have two functions the one is working correctly both in mobile and desktop version of google sheets however another function is not working both in desktop and mobile version through the checkbox the first function is below which is working

function loopThroughQuantity(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var bill = ss.getSheetByName("pad no 1");
var billdata=bill.getRange(8, 1, 27, 6).getValues();
var sheet = ss.getSheetByName("stock");
var range = sheet.getDataRange();
var stock = range.getValues();
for (var i=0; i<billdata.length;i++){
var q = billdata[i][5];
var desc = billdata[i][1];
    for( var j=0; j<stock.length; j++ ) {
      if( stock[j][0] === desc ) {
        range.offset(j,2,1,1).setValue(q);
      }
    }
  }
 }

while the other code is below which is not working through clicking a checkbox

function runBothfunctions(){
//function 1
var ss = SpreadsheetApp.getActiveSpreadsheet()
var ss1 = ss.getSheetByName('pad no 1')
var ss1Id = ss1.getSheetId()
var ss2 = ss.getSheetByName('bill list')
var ssId = ss.getId()
var clientName = ss1.getRange(3,3).getValue()
var invoiceNumber = ss1.getRange(4,3).getValue()
var totalBill = ss1.getRange(29,9).getValue()
var d = new Date();
var fullDate = ('0' + d.getDate()).slice(-2) +"/"+ ('0' + (d.getMonth() +1)).slice(-2) +"/"+ 
(d.getYear().toString()).slice(-2)
var invoiceFileName = invoiceNumber + '_' + fullDate + '_' + clientName +'_'+ totalBill

var url = ss.getUrl()
url = url.replace(/edit$/,'')
var url_ext = 'export?exportFormat=pdf&format=pdf'
+ '&size=A4'                           // paper size: "legal" / "letter" / "A4"
+ '&fitw=true'                         // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false' // hide optional headers and footers
+ '&gridlines=false'                   // hide gridlines
+ '&gid=' + ss1Id                      // the sheet's Id

var token = ScriptApp.getOAuthToken()  
// Convert sheet1 to PDF
var response = UrlFetchApp.fetch(url + url_ext, { headers: { 'Authorization': 'Bearer ' +  token }})

var blob = response.getBlob().setName(invoiceFileName + '.pdf')  
// Add the folder ID of the Drive folder where the PDF should be saved. Create a folder and copy this 
ID: www.goo.gl/TfgqrN
var invoiceURL = 
DriveApp.getFolderById('1I8NvoAERa_NxgS3z5jmYCC8TOlejDHNT').createFile(blob).getUrl()
ss2.getRange(ss2.getLastRow() +1, 1).setFormula('=HYPERLINK("' + invoiceURL + '";"' + invoiceFileName 
+ '")')

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();

//create an array of the invoice numbers already created 
var colArray = sheet.getRange(4, 3).getValues();

//sort the array values to find max
var maxInColumn = colArray.sort(function(a,b){return b-a})[0][0];

var nextCell = sheet.getRange(4, 3);

//assign the max+1 to the next entry
nextCell.setValue(maxInColumn+1);

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Unwanted Sheet');

DriveApp.createFile(ss.getBlob());

//function 2
 var sheet = SpreadsheetApp.getActive().getSheetByName('pad no 1');
sheet.getRange('B8:F28').clearContent();
sheet.getRange('h8:h28').clearContent();
sheet.getRange('C3').clearContent();




//function 3
//below code is for subtracting sold quantity from old quantity
var sheet = SpreadsheetApp.getActive().getSheetByName('stock');

var maxRows = sheet.getMaxRows();

var soldRange = sheet.getRange(2, 3, maxRows); // row, column, number of rows
var totalRange = sheet.getRange(2, 2, maxRows);

var soldValues = soldRange.getValues();
var totalValues = totalRange.getValues();
for (var row in soldValues) {
var soldCellData = soldValues[row][0];
var totalCellData = totalValues[row][0];

if (soldCellData !== "" && totalCellData !== "") {
  totalValues[row][0] = totalCellData - soldCellData;
  soldValues[row][0] = "";
 } 
}

soldRange.setValues(soldValues);
totalRange.setValues(totalValues);


var sheet = SpreadsheetApp.getActive().getSheetByName('pad no 1');
sheet.getRange('c3').setValue('Make Your Next Bill');
 //sheet.getRange('c3').activate();  


//function 4
//var ui = SpreadsheetApp.getUi()
//var response = ui.alert('bill saved and cleared  👍');


}

this code works correctly by clicking an image separately but does not work with onEdit. maybe the code is too long. one thing I add it more here. if it starts working by clicking the checkbox, how to run both functions at one on mobile, do I need to combine these two functions and run it by clicking one checkbox It will be great if these two functions start work with one click. thanks in advance for giving me more time.

Upvotes: 0

Views: 84

Answers (1)

Jeff Rush
Jeff Rush

Reputation: 912

It seems to me that you are overloading your onEdit(e) function.

Take a look on Simple Triggers restrictions

Script executions and API requests do not cause triggers to run.

which is your case

They cannot access services that require authorization.

which is your case too

This is the reason why onEdit(e) is not working.

Upvotes: 0

Related Questions