简体   繁体   中英

RefreshImport function not working in Google Sheets

I tried to fill it with my sheet data, but that

var dataRange = sheet.getDataRange();

get an error. What should I do?

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

  var id = "1r7TgCWXfjmcjufT0yz9qcFDlr482SdHDHlZYblXyAt0";  // [YOUR SPREADSHEET ID]
  var ss = SpreadsheetApp.openById(id);
  var sheet = ss.getSheetByName("crypto"); // 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())
}

I set up the trigger.

Not sure what you trying to do and you explanation leaves a lot to be desired.

So this is a simple way to put your formulas into a sheet named "Destination".

function RefreshImports() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("crypto");
  const rg = sh.getDataRange();
  const formulas = rg.getFormulas();
  sh.getRange(1,1,formulas.length,formulas[0].length).setFormulas(formulas);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM