Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
How to use the result of an IF statement as a variable

I have a code as follows:

function DetailFacture2() {
  var ss = SpreadsheetApp.getActive();
  var DetailDEVIS = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailDEVIS'));
  var FACTUREDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('FACTUREDevis'));
  var DetailFactureDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailFactureDevis'));
  var lastrowpaste = FACTUREDevis.getLastRow();
  var numrow = FACTUREDevis.getRange(lastrowpaste,13).getValue()
  var lastrowpaste2 = DetailFactureDevis.getLastRow() - numrow +2;
  var data = DetailDEVIS.getDataRange().getValues();
  var DetailD = FACTUREDevis.getRange(lastrowpaste,2).getValue();
  for(var i = 0; i<data.length;i++){
    if(data[i][1] == DetailD){ //[1] because column B
      var firstrowcopy = i+1;
      Logger.log(firstrowcopy)
      return (firstrowcopy)
    }
  }
};

It does return the correct value, but how do you use "firstrowcopy" as a fixed var? I would like to use as follows:

function DetailFacture2() {
  var ss = SpreadsheetApp.getActive();
  var DetailDEVIS = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailDEVIS'));
  var FACTUREDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('FACTUREDevis'));
  var DetailFactureDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailFactureDevis'));
  var lastrowpaste = FACTUREDevis.getLastRow();
  var numrow = FACTUREDevis.getRange(lastrowpaste,13).getValue()
  var lastrowpaste2 = DetailFactureDevis.getLastRow() - numrow +2;
  var data = DetailDEVIS.getDataRange().getValues();
  var DetailD = FACTUREDevis.getRange(lastrowpaste,2).getValue();
  for(var i = 0; i<data.length;i++){
    if(data[i][1] == DetailD){ //[1] because column B
      var firstrowcopy = i+1;
      var source = DetailDEVIS.getRange(firstrowcopy,1,numrow-1);
      var destination = DetailFactureDevis.getRange(lastrowpaste2,3);
      source.copyTo(destination);
    }
  }
};

But, as one would expect, it cannot work as it loops...

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Not sure if I understand your question too. The code doesn't look well. Here is just my guess. Try to change the last lines this way:

// ...

  var firstrowcopy = 0;
  for (var i = 0; i < data.length; i++){
    if(data[i][1] == DetailD){ //[1] because column B
      firstrowcopy = i+1;
      break;
    }
  }

  var source = DetailDEVIS.getRange(firstrowcopy,1,numrow-1);
  var destination = DetailFactureDevis.getRange(lastrowpaste2,3);
  source.copyTo(destination);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!