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

162
Visualizações
How can I iterate over an unorganized range and pull data until criteria is not met anymore using Apps Script?

I've been trying to loop over a range of data and bring the data to another sheet until the loop hits one of the following words (Tools, Painting) and stop there.

I was going for a for loop + while, but I'm not sure this would be the best way to do that.

Here's the code draft I'm working on:

function importPipeworkData() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const input4File = ss.getSheetByName('Input Files Mapping').getRange('B4').getValue()
  const pipeworkSheet = SpreadsheetApp.openByUrl(input4File).getSheetByName('Sheet1');
  const inputPipeworkRng = pipeworkSheet.getRange(2, 1, pipeworkSheet.getLastRow(), 4);
  const inputPipeworkData = inputPipeworkRng.getValues();

  const boqPipeworkSheet = ss.getSheetByName('BOQ Pipework');
  const boqPipeworkRng = boqPipeworkSheet.getRange(4, 1, boqPipeworkSheet.getLastRow(), 4);
  //boqPipeworkRng.clear(); //Clears the sheet to receive updated data;

  const pipeworkData = [];

  for (let a = 0; a < inputPipeworkData.length; a++) {
    while (inputPipeworkData[a][0] != 'Valves') //This is one of the unclear parts

      Logger.log(inputPipeworkData[a][1])
  }
}

Here's an example of the dataset: https://docs.google.com/spreadsheets/d/1W9zPt1nYKv59j9kjgFqy5Z4KPgvXZUoCusTk06kRQUk/edit?usp=sharing

I'd appreciate if any help could be given here.

Cheers, Antonio

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In your situation you should use only one loop

Thereby,

  • in case a for loop an additional if statement is necessary .
  • in case of a while loop, you should make sure to increment your counter variable.

Sample with for loop:

  const keyWords = ["Tools", "Painting"];
  for (let a = 0; a < inputPipeworkData.length; a++) {
    if (keyWords.indexOf(inputPipeworkData[a][0]) > - 1){
      break;
     }
      Logger.log(inputPipeworkData[a][1]);
      pipeworkData.push(inputPipeworkData[a]);
  }
}

Sample with while loop:

  const keyWords = ["Tools", "Painting"];
  var a = 0;
  while (keyWords.indexOf(inputPipeworkData[a][0]) == - 1){
    Logger.log(inputPipeworkData[a][1]);
    pipeworkData.push(inputPipeworkData[a]);
    a++
  }
}
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