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

122
Views
Repeat script until conditions are met

I have a google sheet with a timer set for 5am and everyday I want the sheet to:

  1. Refresh, which includes some links to external data, so I add a 10 second pause. There are approximately 100 rows.
  2. I successfully wrote a formula in column U to tell me if data is imported correctly, and if not it returns the text "ERROR".
  3. Search for "ERROR" in column U.
  4. If there are any "ERRORS" in column U, refresh the page again and wait 10 seconds.
  5. Repeat until there are no "ERRORS"
  6. Only when there are no "ERRORS" in column U, copy the values in column V and paste-values in column W.

I think I am close... I just can't figure out how to tell it to re-run the "IF" portion if still finds "ERRORS". Any input is appreciated!

Here is a link to the file -> https://docs.google.com/spreadsheets/d/1GFN3tXRlqxo9J9iNpZMPk-e-WlNGVjJ4zSqZ1_aEE_U

function HardKeyValues() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Players'), true);
  
  SpreadsheetApp.flush();
  Utilities.sleep(10000);
  
  var findVal = spreadsheet.getRange('U:U').getValue()
  if(findVal.match('ERROR')){
  
  SpreadsheetApp.flush();
  Utilities.sleep(10000)}
  
  else{
  spreadsheet.getRange('W:W').activate();
  spreadsheet.getRange('V:V').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);}
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I would not actually use the sleep timer this way

But if I did I would write it like this:

function HardKeyValues() {
  const ss = SpreadsheetApp.getActive();
  const psh = ss.getSheetByName('Players');
  SpreadsheetApp.flush();
  Utilities.sleep(10000);
  const vs = psh.getRange('U1:U' + psh.getLastRow()).getDisplayValues().flat();
  if (vs.find(e => e.includes("ERROR"))) {
    SpreadsheetApp.flush();
    Utilities.sleep(10000);
  } else {
    sh.getRange("V1:V" + sh.getLastRow()).copyTo(sh.getRange("W1:W" + sh.getLastRow(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false));
  }
}

Using indeterminate ranges like "W:W" often leads to many nulls at the end of the array.

about 4 years ago · Juan Pablo Isaza Report

0

What about using a while loop ?

function HardKeyValues() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Players'), true);
  
  SpreadsheetApp.flush();
  Utilities.sleep(10000);
  
  var findVal = spreadsheet.getRange('U:U').getValue()
  
  while (findVal.match('ERROR')){
    SpreadsheetApp.flush();
    Utilities.sleep(10000)}
    findVal = spreadsheet.getRange('U:U').getValue()
  }
  
  spreadsheet.getRange('W:W').activate();
  spreadsheet.getRange('V:V').copyTo(spreadsheet.getActiveRange(), 
  SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}

about 4 years ago · Juan Pablo Isaza Report

0

You could maybe try using a for loop, just make sure to have a variable that can eventually meet the requirement for the for loop to stop, otherwise, it will turn into a forever loop ;)

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!