Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
Google Apps Script - Get a range of cells instead of one cell

I started using Google Apps Script yesterday and I'm struggling in running the code successfully when I attempt to get a range of cells instead of just one cell.

This is the complete code, it will show a pop-up message if the condition is true:

function CheckPlanNumber() {
  // Fetch the monthly sales
  var planNumbersRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Plan Numbers").getRange("B2");
  var planNumbers = planNumbersRange.getValues();
  var ui = SpreadsheetApp.getUi(); 
  // Check totals sales
  if (planNumbers == "XXXX") {
    ui.alert('Contact - Plan Number missing!');
    }
}

However, when I try to extend the range to more cells to go through at least 10 cells and validate them, it doesn't show the pop-up message.

function CheckPlanNumber() {
  // Fetch the monthly sales
  var planNumbersRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Plan Numbers").getRange("B2:B10");
  var planNumbers = planNumbersRange.getValues();
  var ui = SpreadsheetApp.getUi(); 
  // Check totals sales
  if (planNumbers == "XXXX") {
    ui.alert('Contact - Plan Number missing!');
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Range.getValues return values in 2-dimensional array, so if you have a table like this

|     |  A  |      B       |
| --- | --- | ------------ |
| 1   |     | Plan numbers |
| 2   |     |      123     |
| 3   |     |      XXX     |
| 4   |     |      456     |

Range.getValues( 'B2:B4' ) will return

[
      [ "123" ]
    , [ "XXX" ]
    , [ "456" ]
]

You will have to loop through each value and compare them.

var sheet            = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Plan Numbers");
var planNumbersRange = sheet.getRange("B2:B10");
var planNumbers      = planNumbersRange.getValues();
var ui               = SpreadsheetApp.getUi(); 

for ( var row = 0; row < planNumbers.length; row++ ) {
    if ( planNumbers[ row ][ 0 ] == "XXXX" || planNumbers[ row ][ 0 ] == "__" ) {
        ui.alert( 'Contact - Plan Number missing at row ' + ( row + 2 ) );
    }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda