我已经设置了一个流程,用于将CSV从我们的ERP发送到GA帐户并将标签应用于。我想解析附件并更新电子表格,以用作另一个GA项目的数据源。当我试图做任何事情的时候,我总是坚持内环,因为这可能就是我的大脑是有线的。基本上我在电子表格中设置了标签,并为附加的CSV文件设置了一组静态名称,如下所示:
*//Static array values will never change
var sheetNames = ["Wage Codes", "Phase Codes", "Cost Types", "Department"]
var csvName = ["Wage_Codes.csv", "Phase_Codes.csv", "Cost_Types.csv", "Department_Codes.csv"]*
我想循环遍历从csvName导入数据的每个数组,并将单元格值设置为sheetNames
我可以从附加的CSV文件中获取数组,并使用以下内容将其读入Logger:
var label = "Timesheet Data"
var thread = GmailApp.getUserLabelByName(label).getThreads();
//静态数组值永远不会改变 var sheetNames = [“工资代码”,“阶段代码”,“成本类型”,“部门”] var csvName = [“Wage_Codes.csv”,“Phase_Codes.csv”,“Cost_Types.csv”,“Department_Codes.csv”]
//strip CSV attachemnts from emails and put in GDrive
var count = thread[0].getMessageCount();
for (var i = 0 ; i < count; i++) {
var attachments = thread[0].getMessages()[i].getAttachments()[0];
var csvData = Utilities.parseCsv(attachments.getDataAsString(), ",");
Logger.log(csvData);
我可以通过以下方式单独下载:
function importDeptCodes() {
var ss = SpreadsheetApp.openById('sheetID').getSheetByName('Cost Types');
var file = DriveApp.getFilesByName('Cost_Types.csv').next();
var arry = Utilities.parseCsv(file.getBlob().getDataAsString());
ss.getRange(1, 1, arry.length, arry[0].length).setValues(arry);
Logger.log(arry)
}
将它放入第一个代码示例的内循环中是我的大脑放大的地方。
使用以下代码,当yhe j循环上升一级时,它将导入所有内容4次,并且它仅按照现在的方式导入到部门工作表的所有内容
function importCodes() {
var sheetNames = ["Wage Codes", "Phase Codes", "Cost Types", "Department"]
var csvName = ["Wage_Codes.csv", "Phase_Codes.csv", "Cost_Types.csv", "Department_Codes.csv"]
var ss = SpreadsheetApp.openById('sheetID');
for (var i = 0; i < sheetNames.length; i++) {
var ss = SpreadsheetApp.openById('sheetID');
var sheet = ss.getSheetByName(sheetNames.valueOf()[i]);
Logger.log(sheet.getName())
}
for (var j=0; j < csvName.length; j++) {
var file = DriveApp.getFilesByName(csvName.valueOf()[j]).next();
var arry = Utilities.parseCsv(file.getBlob().getDataAsString());
sheet.getRange(1, 1, arry.length, arry[0].length).setValues(arry);
Logger.log(sheet)
}
}
答案 0 :(得分:0)
您可以从附件中获取CSV文件名,找到相应的表格并导入数据。
var attachment = thread[0].getMessages()[i].getAttachments()[0];
var fileIndex = csvName.indexOf(attachment.getName());
if (fileindex !== -1) {
var sheet = ss.getSheetByName(sheetNames[fileIndex]);
// write the import code here
}