【发布时间】:2019-08-24 04:09:58
【问题描述】:
我正在使用 Google API 构建一个脚本,它将新的 CSV 文件附加到 Google 表格报告中。
GDrive 布局:
- Google 表格 = 累计人工报告 > Tab1 = 数据透视表,Tab2 = 数据列表
- 文件夹 = CSV 存储库 > 7 天劳动报告
目前,数据列表每周都会使用新 CSV 文件中的数据手动更新。
流程:
- Web 应用程序将 SQL 报告以 CSV 格式分发到 Gmail 帐户。
- 运行一个时间触发的脚本,将文件保存到 Google Drive 上的 CSV 存储库中。
- *运行时间触发的脚本,将新的 CSV 数据附加到数据列表中
- 新的 CSV 文件已重命名,以在末尾包含日期。
*我在流程中的第 3 步需要帮助。
我尝试了不同的方法,但最接近的方法是添加一个新的数据选项卡,而不是附加数据。
新标签的结果是:
var master = ss.insertnewdata();
这是我目前所拥有的:
function importData() {
// Folder ID of the repository containing all CSV files
var fSource = DriveApp.getFolderById("Folder ID");
// CSV file name of the last recieved report file
var weeklyreport = fSource.getFilesByName('Project Time Reporting.csv');
// Google Sheet ID containing labor report and data list
var ss = Sheet ID');
// Data list tab on Google Sheet
var dataListSheet = ss.getSheetByName('Data List');
// Last row on the data list tab.
var lastRow = dataListSheet.getLastRow();
// var ReportData =
if ( weeklyreport.hasNext() ) { // procced if CSV file name exists in the repository
var file = weeklyreport.next();
var csv = file.getBlob().getDataAsString();
var csvData = CSVToArray(csv); // see below for CSVToArray function
// loop through csv data array and append rows into Data List
for ( var i=0, lenCsv=csvData.length; i<lenCsv; i+i ) {
//After last row on data list, insert new row with all data from CSV.
dataListSheet.getRange(lastRow + 1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
// rename the report.csv file so it is not processed on next scheduled run
file.setName("Weekly Labor Report Export-"+(new Date().toString())+".csv");
}
function CSVToArray( strData, strDelimiter ) {
// Check to see if the delimiter is defined. If not,
// then default to COMMA.
strDelimiter = (strDelimiter || ",");
// Create a regular expression to parse the CSV values.
var objPattern = new RegExp(
(
// Delimiters.
"(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
// Quoted fields.
"(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
// Standard fields.
"([^\"\\" + strDelimiter + "\\r\\n]*))"
),
"gi"
);
// Create an array to hold our data. Give the array
// a default empty first row.
var arrData = [[]];
// Create an array to hold our individual pattern
// matching groups.
var arrMatches = null;
// Keep looping over the regular expression matches
// until we can no longer find a match.
while (arrMatches = objPattern.exec( strData )){
// Get the delimiter that was found.
var strMatchedDelimiter = arrMatches[ 1 ];
// Check to see if the given delimiter has a length
// (is not the start of string) and if it matches
// field delimiter. If id does not, then we know
// that this delimiter is a row delimiter.
if (
strMatchedDelimiter.length &&
(strMatchedDelimiter != strDelimiter)
){
// Since we have reached a new row of data,
// add an empty row to our data array.
arrData.push( [] );
}
// Now that we have our delimiter out of the way,
// let's check to see which kind of value we
// captured (quoted or unquoted).
if (arrMatches[ 2 ]){
// We found a quoted value. When we capture
// this value, unescape any double quotes.
var strMatchedValue = arrMatches[ 2 ].replace(
new RegExp( "\"\"", "g" ),
"\""
);
} else {
// We found a non-quoted value.
var strMatchedValue = arrMatches[ 3 ];
}
// Now that we have our value string, let's add
// it to the data array.
arrData[ arrData.length - 1 ].push( strMatchedValue );
}
// Return the parsed data.
return( arrData );
};
【问题讨论】:
标签: google-apps-script google-api google-api-client