OnEdit to run on 1 sheet and manipulate data on separate sheet

24 views
Skip to first unread message

Marcia W

unread,
Mar 22, 2023, 1:58:56 AM3/22/23
to Google Apps Script Community
Scenario.  I am using Sheets on a tablet so am utilizing OnEdit function.  A drop-down has 2 choices and IF statement in OnEdit then carries out the corresponding task.  A 3rd party app reads and populates data from QR codes into Sheets starting in cell A1. 

Problem.  With just copied and pasted test data, the code worked just fine when both the drop-down and data were on the same sheet.  Then I discovered the 3rd party app added lines as it populated data so the cell with drop-down ends up getting "pushed down" each time data is read.  OnEdit had trigged from a specific cell which was then not valid with this data shift.  I moved the OnEdit drop-down to 1 sheet and the data will be read into a different sheet.  After this "split", the code is not working to carry out the tasks even thought I thought I successfully updated it to activate the sheet containing data in order to carry out tasks.

Current code is below. sheet called "table QRscan" has the drop-down in cell B2 and sheet called "QRscan" is where data is pulled in. If you want to see the original code that worked when both drop-down and data were on 1 sheet, let me know.  TIA for any help you can provide.

function onEdit(e) {
    var wbA = SpreadsheetApp.getActiveSpreadsheet(); //-----gets active Spreadsheet
    var shA = wbA.getActiveSheet(); //-----------gets active sheet
    var shForm = wbA.getSheetByName('tablet QRscan'); //------- identifies "form" sheet
    var shData = wbA.getSheetByName('QRscan'); //------ identifies data sheet
    var sheetName = shA.getName();
    var inputValue = shForm.getRange('B2:B2').getValue;

  if (sheetName === "tablet QRscan" && inputValue === "get total"){
// -------------- get totals and display with formatting ------------
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('QRscan'), true);
  spreadsheet.getRange('A1:A1023').activate();
  spreadsheet.getRange('A1:A1023').splitTextToColumns(SpreadsheetApp.TextToColumnsDelimiter.SEMICOLON);
  spreadsheet.getRange('H1').activate();
  spreadsheet.getCurrentCell().setFormula('=SUM(F:F)');
  spreadsheet.getCurrentCell().setNumberFormat('"$"#,##0.00');
  sspreadsheethData.getRange('I1').activate();
  spreadsheet.getCurrentCell().setValue('TOTAL');
  sspreadsheethData.getRange('H2').activate();
  spreadsheet.getCurrentCell().setFormula('=COUNT(F:F)');
  spreadsheet.getRange('I2').activate();
  spreadsheet.getCurrentCell().setValue('count');
  spreadsheet.getRange('H1:I1').activate();
  spreadsheet.getActiveRangeList().setBackground('#ffff00')
  .setFontWeight('bold');
  spreadsheet.getRange('H2:I2').activate();
  sspreadsheethData.getActiveRangeList().setBackground('#cfe2f3');
  spreadsheet.getRange('H4').activate();
}

  if (sheetName === "tablet QRscan" && inputValue === "reset"){
// ------------ clears data, formulas and drop down entry ------------
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('QRscan'), true);
  spreadsheet.getRange('A:F').activate();
  spreadsheet.setCurrentCell(spreadsheet.getRange('F1'));
  sspreadsheethData.getActiveRangeList().clear({contentsOnlytruecommentsOnlytrueskipFilteredRowstrue});
  spreadsheet.getRange('H:J').activate();
  sspreadsheethData.getActiveRangeList().clearContent();
  spreadsheet.getRange('A5').activate();
}
}  //------closing onEdit
 
Reply all
Reply to author
Forward
0 new messages