3

I want to force an importXML to auto-refresh every five minutes. This is the script I am trying to run and getting the error "Bad value (line 7, file "RefreshImports" . I do not know why. I found it here: Periodically refresh IMPORTXML() spreadsheet function

function RefreshImports() {
  var lock = LockService.getScriptLock();
  if (!lock.tryLock(5000)) return; // Wait up to 5s for previous 
  refresh to end.

  var id = "[YOUR SPREADSHEET ID]";
  var ss = SpreadsheetApp.openById(id);
  var sheet = ss.getSheetByName("[SHEET NAME]");
  var dataRange = sheet.getDataRange();
  var formulas = dataRange.getFormulas();
  var content = "";
  var now = new Date();
  var time = now.getTime();
  var re = /.*[^a-z0-9]import(?:xml|data|feed|html|range)\(.*/gi;
  var re2 = /((\?|&)(update=[0-9]*))/gi;
  var re3 = /(",)/gi;

  for (var row = 0; row < formulas.length; row++) {
    for (var col = 0; col < formulas[0].length; col++) {
      content = formulas[row][col];
      if (content != "") {
        var match = content.search(re);
        if (match !== -1) {
          // import function is used in this cell
          var updatedContent = content.toString().replace(re2, "$2update=" +
            time);
          if (updatedContent == content) {
            // No querystring exists yet in url
            updatedContent = content.toString().replace(re3, "?update=" + time +
              "$1");
          }
          // Update url in formula with querystring param
          sheet.getRange(row + 1, col + 1).setFormula(updatedContent);
        }
      }
    }
  }

  // Done refresh; release the lock.
  lock.releaseLock();

  // Show last updated time on sheet somewhere
  sheet.getRange(7, 2).setValue("Rates were last updated at " +
    now.toLocaleTimeString())
}

In the code where it says "[YOUR SPREADSHEET ID]", I am to enter the name of my spreadsheet correct? I do not know anything about this.

Rubén
  • 34,714
  • 9
  • 70
  • 166
john lemke
  • 31
  • 1
  • 4

2 Answers2

0

On [YOUR SPREADSHEET ID] you should add the spreadsheet id, not it's name.

The spreadsheet id for

https://docs.google.com/spreadsheets/d/1Xhgfr3z4EwPtjS4aahytU_3TOVxjNb8JvHo88h3nZaE/edit#gid=14522064

is

1Xhgfr3z4EwPtjS4aahytU_3TOVxjNb8JvHo88h3nZaE
Rubén
  • 34,714
  • 9
  • 70
  • 166
0

I found it easier to use the URL instead the id, here is the bit of code:

var url = "URL OF SPREADSHEET"; 
var sheetName = "NAME OF SPECIFIC SHEET";
var ss = SpreadsheetApp.openByUrl(url);
var sheet = ss.getSheetByName(sheetName);
Dharman
  • 30,962
  • 25
  • 85
  • 135
sirnejo
  • 1
  • 1