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

375
Views
How to iterate over a range, get a specific word and paste it to an adjacent column until next key word is found in Google Sheets using scripts?

Impressed with how little I know, after hours trying to figure it out, I have to resort to you.

I'm thinking the process should be the following: Define keywords in an array; Iterate over the range; As the keyword is found, it starts counting until next ketword is found; Counter becomes the number of rows for that category; It starts over again with the next keyword found.

I started writing it, but I can't see multiple for loops here and I wonder how else it'd be:

function formatCells() {
  //Utilities.sleep(3000);

  //Formats Categories and cell colors
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const boqPipeworkSheet = ss.getSheetByName('BOQ Pipework');
  const boqPipeworkRng = boqPipeworkSheet.getRange(5, 1, boqPipeworkSheet.getLastRow(), 14);
  const boqPipeworkValues = boqPipeworkRng.getValues();
  let categoryToPaste = [];
  let categories = ['Copper pipework'];
  let counter = 0;
  for (let r = 0; r < boqPipeworkRng.length; r++) {
    let category = boqPipeworkRng[r][0];
    if (categories.indexOf(category) > -1) {
      categoryToPaste.push(category);
      counter++
    }
  }
}

Here's how the data is: enter image description here

Here is a link to a file containing an example: https://docs.google.com/spreadsheets/d/138TD3eJU6GQtRj3uxXBJicn7pZyeXvAqU8lNcU01BDw/edit?usp=sharing

Thank you!

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

0

One solution is to use formula.

For Categories, in F3

={"Category";ArrayFormula(lookup(row(A4:A),row(A4:A)/--REGEXMATCH(B4:B,"^[ ]{2}[A-Z].*"),B4:B))}

For Subcategories, in G3

={"Subcategory";ArrayFormula(if(REGEXMATCH(B4:B,"^[ ]{2}[A-Z].*"),"",lookup(row(A4:A),row(A4:A)/--REGEXMATCH(B4:B,"^[ ]{4}[A-Z].*"),B4:B)))}

enter image description here

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!