使用一点字符串操作,这变得非常容易:
function myFunction() {
var response = UrlFetchApp.fetch("http://research.stlouisfed.org/fred2/data/TWEXMANL.txt");
a=response.getContentText();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var InputSheet = ss.getSheetByName("Sheet1");
//Slice off the text above the columns
var allData = a.slice(a.indexOf("DATE VALUE"));
//Trim spaces between date and value to just 1 space for splitting
allData = allData.replace(/ +/g, " ");
//Split the text so each line is a row
var splitData = allData.split("\n");
//For some reason, there is an extra line, so remove it
splitData.pop();
//Split each row so data is one column and value is another
for(var i = 0; i < splitData.length; i += 1) {
splitData[i] = splitData[i].split(" ");
}
//Save to spreadsheet. +4 because you start at E5. Note the change to F on the right.
InputSheet.getRange("E5:F" + (splitData.length + 4)).setValues(splitData);
}
我首先删除小介绍文字。然后,我将字符串拆分为包含数据行的数组。我也将每一行(一个字符串)分成一个数组。这给了我一个可以setValues()
在底部使用的数组。因为这不仅仅是一个单元格,我必须指定确切的宽度和高度,否则它会失败。
如果您想在其中的任何时候查看数据,我建议Logger.log(splitData);