我创建了一个电子表格,从我家附近的两个河流测量仪中提取数据,以尝试跟踪它们与经过我们时的水位的关系(这是一个旧磨坊),因为这个国家似乎越来越多地被洪水淹没!
基本上,工作表可以完美运行,但在电子表格未打开/关闭笔记本电脑时无法正常工作。我想openById
并且getSheetByName
会解决这个问题,但我没有运气。
ImportXML
当工作表未打开时,我构造的命令仅返回 N/A。该脚本每 15 分钟触发一次。我有另一个基本脚本,它将单元格的内容重写为值,以便一次只有一行ImportXML
命令。这个脚本也是一个定时触发器。
我已经粘贴了下面的脚本 - 如果它们不是很有说服力,请道歉,这是我的第一次尝试!
function fetchData() {
var sheet = SpreadsheetApp.openById("0AgG7jQSg2ljFdGNpbFRjUTA1LXJwdmtPcmxkQnZpUHc").getSheetByName("River Levels");
var bredURL = "http://www.environment-agency.gov.uk/homeandleisure/floods/riverlevels/120743.aspx?stationId=4004";
var belURL = "http://www.environment-agency.gov.uk/homeandleisure/floods/riverlevels/120743.aspx?stationId=4002";
var ssBredLevel = "substring((//div[@class='plain_text']/p)[2],35,4)";
var ssBelLevel = "substring((//div[@class='plain_text']/p)[2],31,4)";
var ssTime = "substring(//div[@class='plain_text']/p[2],34,5)";
var ssDate = "substring(//div[@class='plain_text']/p[2],43,10)";
var commandBredLevel = "ImportXML(\"" + bredURL + "\",\"" + ssBredLevel + "\")"; // build ImportXML() command
var commandBredTime = "ImportXML(\"" + bredURL + "\",\"" + ssTime + "\")"; // build ImportXML() command
var commandBredDate = "ImportXML(\"" + bredURL + "\",\"" + ssDate + "\")"; // build ImportXML() command
var commandBelLevel = "ImportXML(\"" + belURL + "\",\"" + ssBelLevel + "\")"; // build ImportXML() command
var commandBelTime = "ImportXML(\"" + belURL + "\",\"" + ssTime + "\")"; // build ImportXML() command
var commandBelDate = "ImportXML(\"" + belURL + "\",\"" + ssDate + "\")"; // build ImportXML() command
sheet.appendRow(['Bredwardine',' ',' ',' ',' ','Belmont',' ',' ',' ',' ','=now()']); // append row
var lastrow = sheet.getLastRow(); // define last row
var bredLevel = sheet.getRange(lastrow, 2); // define ImportXML cell
var bredTime = sheet.getRange(lastrow, 3); // define ImportXML cell
var bredDate = sheet.getRange(lastrow, 4); // define ImportXML cell
var belLevel = sheet.getRange(lastrow, 7); // define ImportXML cell
var belTime = sheet.getRange(lastrow, 8); // define ImportXML cell
var belDate = sheet.getRange(lastrow, 9); // define ImportXML cell
bredLevel.setFormula(commandBredLevel);
bredTime.setFormula(commandBredTime);
bredDate.setFormula(commandBredDate);
belLevel.setFormula(commandBelLevel);
belTime.setFormula(commandBelTime);
belDate.setFormula(commandBelDate);
writeFormats();
}
function writeRows() {
var sheet = SpreadsheetApp.openById("0AgG7jQSg2ljFdGNpbFRjUTA1LXJwdmtPcmxkQnZpUHc").getSheetByName("River Levels");
var range = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
range.setValues(range.getValues());
};
function writeFormats() {
var sheet = SpreadsheetApp.openById("0AgG7jQSg2ljFdGNpbFRjUTA1LXJwdmtPcmxkQnZpUHc").getSheetByName("River Levels");
var lastcol = sheet.getLastColumn(); // define width of sheet
var lastrow = sheet.getLastRow(); // define last row
var penrow = lastrow-1; // define penultimate row
var formatting = sheet.getRange(penrow,1,1,lastcol); // fetch formatting from penultimate row
formatting.copyFormatToRange(sheet,1,lastcol,lastrow,lastrow); // paste formatting to last row
};