Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda