2017-08-26 588 views
0

我正在尝试使Google床单应用程序脚本适用于我正在使用的IMPORTXML。将IMPORTXML添加到自动更新的Google Apps脚本

A1 
=importxml("http://www.nfl.com/liveupdate/scorestrip/ss.xml","//@q") 

A2 
=importxml("http://www.nfl.com/liveupdate/scorestrip/ss.xml","//@h") 

数据从A1:B16

填充根据剧本我在网上发现有它自动刷新:

function getData() { 
    var queryString = Math.random(); 

    var cellFunction1 = '=IMPORTXML("' + SpreadsheetApp.getActiveSheet().getRange('A1').getValue() + '?' + queryString + '","'+ SpreadsheetApp.getActiveSheet().getRange('A2').getValue() + '")'; 
    SpreadsheetApp.getActiveSheet().getRange('C1').setValue(cellFunction1); 

    var cellFunction2 = '=IMPORTXML("' + SpreadsheetApp.getActiveSheet().getRange('A4').getValue() + '?' + queryString + '","'+ SpreadsheetApp.getActiveSheet().getRange('A5').getValue() + '")'; 
    SpreadsheetApp.getActiveSheet().getRange('C2').setValue(cellFunction2); 
} 

我不知道我应该是把/用我的代码代替。如果有人能够帮助我解释我应该改变什么来让它在我的工作表中发挥作用/提供一些示例,说明如何看待这将是一个巨大的帮助。

我很欣赏

+0

[定期刷新IMPORTXML()电子表格函数](https://stackoverflow.com/questions/33872967/periodically-refresh-importxml-spreadsheet-function)可能的副本 –

回答

0

您可以通过在time-driven trigger功能使用getFormula()然后setFormula()更新的功能。下面是相关SO post的代码片段:

/** 
* 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(); 
    } 
} 

希望这有助于。

相关问题