Home > Mobile >  Google Sheet Script - if else, checking if cells match
Google Sheet Script - if else, checking if cells match

Time:07-06

looking for some help with the function below. I'm trying to have it check if a file has been updated in Google Drive before running a import script. I have it down to checking if two dates/times match in a sheet, but I can't seem to get it to correctly register whether they match. It should either be when S3 <> T3 or when U3 = FALSE. Any help would be greatly appreciated!!

function syncCSVtransactions() {
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var sh = ss.getSheetByName("LOOKUP")
  var cell_trnsnew = sh.getRange("S3");
  var cell_trnsold = sh.getRange("T3");
  var cell_trnscheck = sh.getRange("U3");
  if( cell_trnsnew != cell_trnsold ){    //this is the line giving trouble
    var source_file = DriveApp.getFilesByName("data_export.csv").next();
    var csvData = Utilities.parseCsv(source_file.getBlob().getDataAsString());
    var sheet2 = ss.getSheetByName('trs');
    sheet2.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
    cell_trnsnew.copyTo(cell_trnsold, {contentsOnly:true});
    chartupdate();
  } else {
    }
}

CodePudding user response:

I think that in your script, var cell_trnsnew = sh.getRange("S3");, var cell_trnsold = sh.getRange("T3"); and var cell_trnscheck = sh.getRange("U3"); can be written by one call. And, although I'm not sure about the values of your "LOOKUP" sheet, how about the following 2 patterns?

Pattern 1:

In this pattern, it supposes that the values of "S3", "T3" and "U3" are the date object, the date object and boolean, respectively.

From:

var cell_trnsnew = sh.getRange("S3");
var cell_trnsold = sh.getRange("T3");
var cell_trnscheck = sh.getRange("U3");
if( cell_trnsnew != cell_trnsold ){

To:

var [cell_trnsnew, cell_trnsold, cell_trnscheck] = sh.getRange("S3:U3").getValues()[0];
if (cell_trnsnew.getTime() != cell_trnsold.getTime() || cell_trnscheck === false) {

Pattern 2:

In this pattern, the values of "S3", "T3" and "U3" are used as the string values.

From:

var cell_trnsnew = sh.getRange("S3");
var cell_trnsold = sh.getRange("T3");
var cell_trnscheck = sh.getRange("U3");
if( cell_trnsnew != cell_trnsold ){

To:

var [cell_trnsnew, cell_trnsold, cell_trnscheck] = sh.getRange("S3:U3").getDisplayValues()[0];
if (cell_trnsnew != cell_trnsold || cell_trnscheck == "FALSE") {

References:

  • Related