简体   繁体   English

谷歌表格导入HTML

[英]Google Sheets IMPORTHTML

I have completed the following script.我已经完成了以下脚本。 I want to get a table in my Google Sheet with IMPORTHTML, everything works fine, only if the data changes, then that doesn't change in the Google table.我想在我的 Google Sheet 中使用 IMPORTHTML 获取一个表格,一切正常,只有当数据发生变化时,Google 表格中才会发生变化。 I run the whole thing with a trigger so that will be updated every minute.我用触发器运行整个过程,以便每分钟更新一次。 But here comes the problem, if I let the whole thing run like this and the data changes on the website, and here comes now the problem that doesn't change in the Google Sheet, the old values ​​are simply taken over and over again and not the new ones.但是问题来了,如果我让整个事情这样运行,网站上的数据发生变化,而现在出现了Google Sheet中没有变化的问题,旧值只是一遍遍地取而代之再次而不是新的。 If I delete IMPORTHTML from the cell manually and then copy it back in, it works strangely.如果我手动从单元格中删除 IMPORTHTML 然后将其复制回,它会奇怪地工作。 But unfortunately it doesn't work with the script and the trigger, what am I doing wrong?但不幸的是它不适用于脚本和触发器,我做错了什么?

function getData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Lager123");
    
  var importXpath_1 = '=IMPORTHTML("URLwithSIMPLEtable","table", 1)';
  
  sheet.getRange("A1").setValue(importXpath_1);
  
}

Clear the content of the cell and then flush() it before you set the value:在设置值之前清除单元格的内容然后flush()它:

function getData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Lager123");
    
  var importXpath_1 = '=IMPORTHTML("URLwithSIMPLEtable","table", 1)';
  sheet.getRange("A1").clearContent();
  SpreadsheetApp.flush();
  sheet.getRange("A1").setValue(importXpath_1);
  
}

References:参考:

For not getting cached response in importhtml, we can append some random query parameter in URL string.为了不在 importhtml 中获得缓存响应,我们可以在 URL 字符串中附加一些随机查询参数。

var queryString = Math.random(); // This will generate random value
  sheetName.getRange('P7').setValue(queryString); // This random value will be placed in Cell P7

  var cellFunction = '=ImportHtml("URL?"&P7;"table";1)}'; // Value from P7 cell will be appended in URL to avoid caching issue
    sheetName.getRange('A1').setValue(cellFunction); 

I tried this in my project and it's working perfectly.我在我的项目中试过这个,它工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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