Google apps script Google Drive上带有CSV存储库的Google工作表报告>将新CSV添加到存储库时更新报告 我正在使用GoogleAPI构建一个脚本,将新的CSV文件附加到GoogleSheet报告中。 GDrive布局: Google Sheet=累计人工报告>Tab1=透视表,Tab2=数据列表 文件夹=CSV存储库>7天人工报告
目前,每周都会使用新CSV文件中的数据手动更新数据列表 过程: Web应用程序将SQL报表作为CSV分发到Gmail帐户。 运行时间触发脚本,将文件保存到Google Drive上的CSV存储库。 *运行时间触发脚本,将新CSV数据附加到数据列表 新的CSV文件将被重命名,以便在末尾包含日期。 *我需要过程中步骤3的帮助。 我尝试过不同的方法,但最接近的方法是添加一个新的数据选项卡,而不是附加数据 新选项卡是以下原因导致的:Google apps script Google Drive上带有CSV存储库的Google工作表报告>将新CSV添加到存储库时更新报告 我正在使用GoogleAPI构建一个脚本,将新的CSV文件附加到GoogleSheet报告中。 GDrive布局: Google Sheet=累计人工报告>Tab1=透视表,Tab2=数据列表 文件夹=CSV存储库>7天人工报告,google-apps-script,google-api,google-api-client,Google Apps Script,Google Api,Google Api Client,目前,每周都会使用新CSV文件中的数据手动更新数据列表 过程: Web应用程序将SQL报表作为CSV分发到Gmail帐户。 运行时间触发脚本,将文件保存到Google Drive上的CSV存储库。 *运行时间触发脚本,将新CSV数据附加到数据列表 新的CSV文件将被重命名,以便在末尾包含日期。 *我需要过程中步骤3的帮助。 我尝试过不同的方法,但最接近的方法是添加一个新的数据选项卡,而不是附加数据 新选项卡是以下原因导致的: var master = ss.insertnewdata();
var master = ss.insertnewdata();
这就是我到目前为止所做的:
在importData函数中,定义一个变量作为数据列表选项卡:
查找“数据列表”选项卡的最后一行:
将导入的数据添加到lastRow下面的行:
谢谢,它现在正在将信息添加到数据列表选项卡,但不幸的是,现在返回时出现错误:执行失败:数据中的列数与范围中的列数不匹配。数据有1,但范围有12行。请参阅更新的脚本。错误阻止重命名.csv文件。您需要记录csv数组并检查它,以了解脚本在列数方面不匹配的原因。如果要将导入的数据重新添加到图纸中,则已将其放置在循环中。但这一行可以一次性发布整个CSV阵列。在循环内部,它只是重复粘贴作业。
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 );
};
var dataListSheet = ss.getSheetByName('Data List');
var lastRow = dataListSheet.getLastRow();
dataListSheet.getRange(lastRow + 1, 1, csvData.length, csvData[0].length).setValues(csvData)