我的google工作表报告需要两个方面的帮助。我会解释一下。
我的谷歌工作表脚本将数据从csv文件发送到谷歌工作表文件制作报告,到目前为止工作得很好没有问题。但唯一的问题是每天我必须手动添加新的cvs文件名,以便我的脚本抓取csv数据并将数据发送到新的google工作表,以便每天早上9点从触发器进行报告。所以我想知道是否有办法,而不是我手动添加新的csv名称,脚本可以找到它并添加它。
所以在这里我必须在我的触发器运行之前手动添加新的csv文件名,我想看看是否而不是我添加它,脚本可以为我做。
var fi = fSource.getFilesByName('201707230600060843.csv'); //最新报告文件
这是我的剧本
function importData() {
var fSource = DriveApp.getFolderById("0ByXeCX01JfKJN1dTNk1SRlQyb1k"); // reports_folder_id = id of folder where csv reports are saved
var fi = fSource.getFilesByName('201707230600060843.csv'); // latest report file
var ss = SpreadsheetApp.openById("1T2JU4KwpJsnlJk0LOEZoHr9uqnNrVYwBWI1NxOwL4PU"); // data_sheet_id = id of spreadsheet that holds the data to be updated with new report data
if ( fi.hasNext() ) { // proceed if "report.csv" file exists in the reports folder
var file = fi.next();
var csv = file.getBlob().getDataAsString();
var csvData = CSVToArray(csv); // see below for CSVToArray function
//var newsheet = ss.insertSheet("NEWDATA"); // create a 'NEWDATA' sheet to store imported data
var sheetname = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy");
var newsheet = ss.insertSheet(sheetname);
// loop through csv data array and insert (append) as rows into 'NEWDATA' sheet
for ( var i=0, lenCsv=csvData.length; i<lenCsv; i++ ) {
newsheet.getRange(i+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i]));
}
/*
** report data is now in 'NEWDATA' sheet in the spreadsheet - process it as needed,
** then delete 'NEWDATA' sheet using ss.deleteSheet(newsheet)
*/
// rename the report.csv file so it is not processed on next scheduled run
file.setName("report-"+(new Date().toString())+".csv");
}
};
答案 0 :(得分:0)
我进行了设置,只要它具有扩展名.csv,新文件名是什么并不重要。最后,我将所有文件的扩展名更改为旧版,以便第二天不再对它们进行重新处理。
function importData(folderID)
{
var ss = SpreadsheetApp.openById("1T2JU4KwpJsnlJk0LOEZoHr9uqnNrVYwBWI1NxOwL4PU");
var folderID = (typeof(folderID) !== 'undefined')? folderID : '0ByXeCX01JfKJN1dTNk1SRlQyb1k';//default folderID
var fldr = DriveApp.getFolderById(folderID);
var files = fldr.getFiles();
var dt = Utilities.formatDate(new Date(), Session.getScriptTimeZone(),"yyyyMMdd");
var re =new RegExp('^' + dt + '.*\.csv$','i');
while (files.hasNext())
{
var file = files.next();
var filename = file.getName();
if(filename.match(re))
{
var csvstr=file.getBlob().getDataAsString();
var csvA=Utilities.parseCsv(csvstr);
var sheetname = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "M/dd/yyyy'-'HH:mm:ss");
var newsheet = ss.insertSheet(sheetname);
newsheet.getRange(1, 1, csvA.length, csvA[0].length).setValues(csvA);
file.setName(filename.slice(0,-3) + 'old'); //the .old files won't pass the file re filter next time.
}
}
}