Home > Software design >  Inserting an IF condition
Inserting an IF condition

Time:05-24

I have the following script which imports rows to an Active sheet from the Source sheet. After a row is imported that row is labelled "IMPORTED" own the source sheet. I require an IF condition so that if a row has been labelled "IMPORTED" this row will not copy to the Active Sheet. The script works perfect without the IF condition, but with the IF condition the script fails to run.

  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Import Dockets')
    .addItem('Import Dockets','deliveries')
    .addToUi();
}
function deliveries(){
  // Gets the active sheet.
  var sheet = SpreadsheetApp.getActiveSheet();
  
  // Gets a different spreadsheet from Drive using
  // the spreadsheet's ID. 
  var bookSS = SpreadsheetApp.openById(
    "SHEETID" 
  );

  // Gets the sheet, data range, and values of the
  // spreadsheet stored in bookSS.
  var bookSheet = bookSS.getSheetByName("DOCKETS");
  var lr = bookSheet.getLastRow();
  var bookRange = bookSheet.getRange(1,1,lr,58);
  var bookListValues = bookRange.getValues();

  // Add those values to the active sheet in the current
  // spreadsheet. This overwrites any values already there.
  var lr = bookSheet.getLastRow();
  
  bookListValues.forEach((r, i) => {
  var impor = r[56];  
  if (impor != 'IMPORTED'){
  sheet.getRange(1, 1, bookRange.getHeight(), bookRange.getWidth()) 
    .setValues(bookListValues)

  var lr = bookSheet.getLastRow();
  bookSheet.getRange(2,57,lr,1).setValue("IMPORTED");
  
  // Rename the destination sheet and resize the data
  // columns for easier reading.
  sheet.setName("DOCKETS");
  sheet.autoResizeColumns(1, 25);

  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('DOCKETS'); // adjust this to the name of your sheet
  const values = sh.getRange('F2:F' sh.getLastRow()).getValues().flat().map(v=>[v.slice(0,v.indexOf(','))]);
  
  sh.getRange(2,9,values.length,1).setValues(values); // paste them in column B
   
}

Any assistance is much appreciated. Thanks

CodePudding user response:

Try it this way

function deliveries() {
  const ash = SpreadsheetApp.getActiveSheet();
  const dss = SpreadsheetApp.openById("SHEETID");
  const dsh = dss.getSheetByName("DOCKETS");
  const brg = dsh.getRange(1, 1, dsh.getLastRow(), 58);
  const bvs = brg.getValues();
  let arr = [];
  bvs.forEach((r, i) => {
    if (r[56] != 'IMPORTED') {
      arr.push(r);
    }
  })
  if(arr) {
    ash.setName("DOCKETS")
    ash.getRange(2,1,a.length,a[0].length).setValues(a);
  }
}
  • Related