[英]Need google apps script to iterate through an array
I have modified this code I found but i cannot figure out how to get it to run for the whole spreadsheet. 我已经修改了找到的代码,但无法弄清楚如何使其在整个电子表格中运行。
the modified code copies a spreadsheet, renames it based on another spreadsheet and then files the document in the appropriate folder. 修改后的代码将复制一个电子表格,然后基于另一个电子表格对其进行重命名,然后将文档归档到适当的文件夹中。
function generateMonthlyReport() {
var data = SpreadsheetApp.openById(CUSTOMER_SPREADSHEET);
// Fetch variable names
// they are column names in the spreadsheet
var sheet = data.getSheets()[0];
var columns = getRowAsArray(sheet, 1);
Logger.log("Processing columns:" + columns);
var customerData = getRowAsArray(sheet, CUSTOMER_ID);
Logger.log("Processing data:" + customerData);
// Assume first column holds the name of the customer
var customerName = customerData[1];
var customerCityState = customerData[2];
SpreadsheetApp.getActiveSheet().getSheetId()
var target = createDuplicateDocument(SOURCE_TEMPLATE, "CSMR - " + customerName + ", " + customerCityState );
Logger.log("Created new document");
}
not sure I understood exactly what you wanted to get as a final result but here is how the loop could be built, it will create a doc for each row of data. 不确定我是否确切地了解最终结果是什么,但是这是循环的构建方式,它将为每一行数据创建一个文档。
function generateMonthlyReport() {
var data = SpreadsheetApp.getActiveSpreadsheet();
// Fetch variable names
// they are column names in the spreadsheet
var sheet = data.getSheets()[0];
var columns = getRowAsArray(sheet, 1);
for(var customer=2;customer<sheet.getLastRow();customer++){
Logger.log("Processing row:" + customer);
var customerData = getRowAsArray(sheet, customer);
Logger.log("Processing data:" + customerData);
// Assume first column holds the name of the customer
var customerName = customerData[0];
var customerCityState = customerData[2];
SpreadsheetApp.getActiveSheet().getSheetId()
// var target = createDuplicateDocument(SOURCE_TEMPLATE, "CSMR - " + customerName + ", " + customerCityState );
Logger.log("Created new document for customer "+customer+' ctitle = CSMR - ' + customerName + ', ' + customerCityState);
}
}
function getRowAsArray(sheet, n){
var array = [];
var data = sheet.getRange(n,1,1,sheet.getLastColumn()).getValues();
for(var c=0;c<data[0].length;c++){
array.push(data[0][c]);
}
return array;
}
Logger of the test + test sheet below : 下面的测试+测试表的记录器:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.