Reputation: 2107
I am importing data from a .CSV file
function myFunction() {
var url = "https://projects.fivethirtyeight.com/soccer-api/club/spi_matches.csv";
var data = UrlFetchApp.fetch(url).getContentText();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var resource = {requests: [{pasteData: {
data: data,
coordinate: {sheetId: sheet.getSheetId()},
delimiter: ","
}}]};
Sheets.Spreadsheets.batchUpdate(resource, ss.getId());
}
The data from past dates does not interest me and I would like to filter to make the sheet smaller and also to make my work easier, via script I could not filter directly from the data source and when I try to filter by a formula in another page, the filter does not work.
=FILTER('Page1'!A2:A,'Page1'!A2:A>=TEXT(now(),"dd/mm/yyyy"))
I would like to know what options I have to be able to filter this data, because I will update it daily, and this filter is very important for the study I am going to do.
If there is any way to filter already at import time, it would be even better, but if i need to use functions on another spreadsheet page, no problem either.
Upvotes: 0
Views: 640
Reputation: 201358
If my understanding is correct, how about this modification? Please think of this as just one of several answers.
In this modification, the following flow is used.
UrlFetchApp.fetch()
.Utilities.parseCsv()
.filter()
.setValues()
.function myFunction() {
var url = "https://projects.fivethirtyeight.com/soccer-api/club/spi_matches.csv";
var data = UrlFetchApp.fetch(url).getContentText();
var csv = Utilities.parseCsv(data);
var d = new Date();
d.setHours(0, 0, 0, 0);
var today = d.getTime();
var values = csv.filter(function(e, i) {
var temp = new Date(e[0]);
temp.setHours(0, 0, 0, 0);
return i > 0 && temp.getTime() >= today;
});
values.unshift(csv[0]);
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
If I misunderstood your question and this was not the result you want, I apologize.
Upvotes: 1