0

I'm using Google Sheet's IMPORTDATA function to grab information from an XML file that is pulling from an API but the information I pull into the sheet isn't up to date.

How can I modify my sheet to pull in up-to-date data?

Compare the sheet: https://docs.google.com/spreadsheets/d/1W0Bt5z-Tky-tNhG_JtfE4FfjTRgQNRu_eQu2qVhQ-_E/edit?usp=sharing (LiveScores sheet)

To the XML: https://www67.myfantasyleague.com/2019/export?TYPE=liveScoring&L=64741&APIKEY=&W=14&DETAILS=1&JSON=0

Observe franchise id="0015" in both sets of data.

The sheet states <franchise id="0005" score="0.00" gameSecondsRemaining="21600" playersYetToPlay="6" playersCurrentlyPlaying="0" isHome="0">

The XML states <franchise id="0015" score="11.14" gameSecondsRemaining="20004" playersYetToPlay="4" playersCurrentlyPlaying="2"> (This data is for a football game that is currently being played as I'm writing this so the above example may not be exact, but it WON'T be score of 0.00, for example.

Any help would be amazing, thanks!

player0
  • 124,011
  • 12
  • 67
  • 124
Amod
  • 71
  • 2
  • 9

2 Answers2

2

Have you tried using IMPORTXML? Google Sheets IMPORTXML Page

In IMPORTXML, you can just use the Inspect Element feature to pull the xpath.

Hope this helps. Let me know if I can help further.

Edit: Instructions To Change When Data Is Imported

  1. In the toolbar go to the script editor
  2. Now in the scripts, paste the code listed bellow

/**
 * Go through all sheets in a spreadsheet, identify and remove all spreadsheet
 * import functions, then replace them a while later. This causes a "refresh"
 * of the "import" functions. For periodic refresh of these formulas, set this
 * function up as a time-based trigger.
 *
 * Caution: Formula changes made to the spreadsheet by other scripts or users
 * during the refresh period COULD BE OVERWRITTEN.
 *
 * From: https://stackoverflow.com/a/33875957/1677912
 */
function RefreshImports() {
  var lock = LockService.getScriptLock();
  if (!lock.tryLock(5000)) return;             // Wait up to 5s for previous refresh to end.
  // At this point, we are holding the lock.

  var id = "YOUR-SHEET-ID";
  var ss = SpreadsheetApp.openById(id);
  var sheets = ss.getSheets();

  for (var sheetNum=0; sheetNum<sheets.length; sheetNum++) {
    var sheet = sheets[sheetNum];
    var dataRange = sheet.getDataRange();
    var formulas = dataRange.getFormulas();
    var tempFormulas = [];
    for (var row=0; row<formulas.length; row++) {
      for (col=0; col<formulas[0].length; col++) {
        // Blank all formulas containing any "import" function
        // See https://regex101.com/r/bE7fJ6/2
        var re = /.*[^a-z0-9]import(?:xml|data|feed|html|range)\(.*/gi;
        if (formulas[row][col].search(re) !== -1 ) {
          tempFormulas.push({row:row+1,
                             col:col+1,
                             formula:formulas[row][col]});
          sheet.getRange(row+1, col+1).setFormula("");
        }
      }
    }

    // After a pause, replace the import functions
    Utilities.sleep(5000);
    for (var i=0; i<tempFormulas.length; i++) {
      var cell = tempFormulas[i];
      sheet.getRange( cell.row, cell.col ).setFormula(cell.formula)
    }

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

This snippet of code came from Periodically refresh IMPORTXML() spreadsheet function

  1. Last and definitely the least, replace the "YOUR-SHEET-ID"

NOTE: I have not personally tested this code and I cannot vouch for it. I recommend making a copy and testing it there first.

Hopefully, this solves the issue of your data not being imported as often as you want. If you want to manually get "fresh" data, you can just delete/cut the import function and paste it back in.

JVKX
  • 58
  • 1
  • 11
  • thanks for the offer! I would love your help! Do you know if IMPORTXML would give me "fresher" data? I want to figure out why google sheets isn't getting up-to-date data before digging into changing my import function. – Amod Dec 06 '19 at 02:39
  • 1
    Hello again! The rate at which "fresh" data is imported, is not able to be changed unless using the Google Apps Script. I am editing my answer now with the instructions and code to change the refresh rate – JVKX Dec 06 '19 at 04:34
  • @Amod I'm back and while I was playing around with my own spreadsheet, I noticed a setting under File>Spreadsheet Settings>Calculation called Recalculation. I do not know if this applies to your use case but you can change formulas to recalculate either when something changes in the spreadsheet or every minute. I don't know if I made that much sense but it might be worth a look. – JVKX Jan 11 '20 at 04:20
0

try in A2:

=ARRAYFORMULA(IFNA(VLOOKUP(C2:C, PlayerList!A:F, {2, 6}, 0)))

and in C2:

=ARRAYFORMULA(QUERY(REGEXEXTRACT(QUERY(IMPORTDATA(
 "https://www67.myfantasyleague.com/2019/export?TYPE=liveScoring&L=64741&APIKEY=&W=14&DETAILS=1&JSON=0?273"), 
 "where Col1 contains 'player id'", 0), 
 "(player id=""(\d+)).+?(score=""(\d+.\d+))"), 
 "select Col2,Col4"))

0

spreadsheet demo

Community
  • 1
  • 1
player0
  • 124,011
  • 12
  • 67
  • 124
  • Thank you player0! Can you explain how this answer addresses the frequency of getting fresh data? I am seeing this as more of a format change but I'm obviously missing something. Thanks again for your previous help as well! – Amod Dec 07 '19 at 05:49
  • 1
    you are 99,9% right. unless you have too many formulas in your sheet which could cause calculation slowdown eg. not getting fresh data right away but laters – player0 Dec 07 '19 at 08:15