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

191
Views
How do I use Google Script If Statements?

I am familiar with Excel VBA but am new to Google Script Editing

I am trying to use a simple IF statement to insert a new line above line 2 if cells A2 & B2 are NOT Blank.

  function AddaLine() {
if (B1 == false)
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('2:2').activate();
  spreadsheet.getActiveSheet().insertRowsBefore(spreadsheet.getActiveRange().getRow(), 1);
  spreadsheet.getActiveRange().offset(0, 0, 1, spreadsheet.getActiveRange().getNumColumns()).activate();
  spreadsheet.getRange('A2').activate();
};

where cell B1 contains

=isblank(A2:B2)

or

function AddaLine() {
  if (A2, B2 != null)
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('2:2').activate();
  spreadsheet.getActiveSheet().insertRowsBefore(spreadsheet.getActiveRange().getRow(), 1);
  spreadsheet.getActiveRange().offset(0, 0, 1, spreadsheet.getActiveRange().getNumColumns()).activate();
  spreadsheet.getRange('A2').activate();
};

Every combination I can think of returns errors. Any help would be appreciated.

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

0

I believe your goal is as follows.

  • From I am trying to use a simple IF statement to insert a new line above line 2 if cells A2 & B2 are NOT Blank., you want to insert a new row to the row 2 when the cells "A2" and "B2" are not empty.

In this case, how about the following modification?

Modified script:

function AddaLine() {
  var spreadsheet = SpreadsheetApp.getActive();
  var [a2, b2] = spreadsheet.getActiveSheet().getRange("A2:B2").getDisplayValues()[0];
  if (a2 != "" && b2 != "") {
    spreadsheet.getRange('2:2').activate();
    spreadsheet.getActiveSheet().insertRowsBefore(spreadsheet.getActiveRange().getRow(), 1);
    spreadsheet.getActiveRange().offset(0, 0, 1, spreadsheet.getActiveRange().getNumColumns()).activate();
    spreadsheet.getRange('A2').activate();
  }
}

Or, for example, as a simple script, how about the following modification?

function AddaLine() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var [a2, b2] = sheet.getRange("A2:B2").getDisplayValues()[0];
  if (a2 != "" && b2 != "") {
    sheet.insertRowBefore(2);
  }
}
  • In order to check whether the cells "A2" and "B2" are not empty, I compared the values retrieved from the cells.

Note:

  • As the additional information by advising from Rubén's comment, in your script, you use the following script.

      if (A2, B2 != null)
      var spreadsheet = SpreadsheetApp.getActive();
      spreadsheet.getRange('2:2').activate();
    

    In this case, when the the condition is true, only the line of var spreadsheet = SpreadsheetApp.getActive(); is run. When you want to use the script below if (A2, B2 != null), please enclose them by {}. Ref

about 4 years ago · Juan Pablo Isaza Report

0

Rather for educational purpose. If you want to check all elements of an array you can use every() method:

function AddaLine() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getRange("A2:B2").getDisplayValues().flat(); 
  if (data.every(x => x != '')) sheet.insertRowBefore(2);
}
  • JavaScript Array every()

Alternatively, in your case, you can use some() to check if some of the elements is empty:

function AddaLine() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getRange("A2:B2").getDisplayValues().flat();
  if (data.some(x => x == '')) return; // do nothing if there is an empty cell
  sheet.insertRowBefore(2);
}
  • JavaScript Array some()

Probably somme() will be a little bit more efficient since it doesn't need to check all elements of the array.

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!