2

I have the script below which is importing some XML data in a google sheet called prices. Everything works fine except that I have set up a time driven trigger to run every minute but the data won't get updated.

  • The trigger seems to work fine, as I can see the last run time being updated every minute.
  • The script calling the XML data works fine as I can see the data being populated in the spreadsheet.
  • The XML feed works fine too, as I can see the time being updated every minute, also have a cron job.
  • I only have this function as a project.

    function getData() {
      var sheetName = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("prices");
      var queryString = Math.random();
      var cellFunction = '=ImportXML("http://myxmldata.com/data-xml.php","//data/date")';
      sheetName.getRange('A2').setValue(cellFunction);
    } 
    

    So what's wrong?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Seb
  • 145
  • 1
  • 3
  • 11
  • Thank you for replying. I'm glad your issue was resolved. From your replying, I could understand that my answer didn't resolve your issue. So I have to delete my answer. Because I don't want to confuse other users. This is due to my poor skill. I deeply apologize for this. By the way, can you post your answer as an answer? By this, it will be useful for other users who have the same issue. – Tanaike Mar 14 '20 at 23:23

1 Answers1

5

Here is how I solved my problem:

On your spreadsheet go to the top menu > click Tool > then Script Editor and add the following scripts:

This is the script to call your data e.g. XML. Please update the script with your own information. YOUR-SHEET-NAME, is the tab name e.g. "prices".

function getData() {
  var sheetName = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("YOUR- 
  SHEET-NAME");
  var queryString = Math.random();
  var cellFunction = '=ImportXML("https://yoururl-xml.php","//trade/price")';  

  var range = sheetName.getRange('A2');

  range.clearContent();  // You can also use range.setFormula("");
  SpreadsheetApp.flush();
  range.setFormula(cellFunction);
  }

Below that script, add the following script, more information on this page: Periodically refresh IMPORTXML() spreadsheet function

YOUR-SHEET-ID is the long number in the spreadsheet url e.g. 1YTB12xSTMSNdoT_S1U67MtOUDTf6n4OL2tJLnTNAXYZ

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

  var id = "YOUR-SHEET-ID";
  var ss = SpreadsheetApp.openById(id);
  var sheet = ss.getSheetByName("YOUR-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();

}

Here is a screenshot of both scripts:

enter image description here

Then add the timer, go to the top menu click on the clock and add trigger. Make sure to select the right function i.e. RefreshImports.

enter image description here

enter image description here

Done!

Seb
  • 145
  • 1
  • 3
  • 11