简体   繁体   English

需要Google Apps脚本来遍历数组

[英]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.

相关问题 通过从 Google Sheet 生成的数组进行交互 | Google Apps 脚本 - Interact through an array generated from a Google Sheet | Google Apps Script 循环遍历多维数组并拆分 | Google Apps 脚本 - Loop through multidimensional array and split | Google Apps Script 挑战循环遍历数组以删除 Google Apps 脚本中的行 - Challenge Looping through an array to delete rows in Google Apps Script 迭代 Google Apps 脚本中的对象 - Iterate over an object in Google Apps script 如何遍历多个URL以在Google Apps脚本中获取JSON响应? - How can I iterate through multiple URLs to fetch JSON response in Google Apps Script? 如何定位和迭代 Google 表格/应用程序脚本中的一个特定列(按列名)? - How to target and iterate through just one specific column (by column name) in Google sheets / apps script? Google 应用程序脚本 - 迭代文件夹和子文件夹 - Google apps script - iterate folder and subfolder Google Apps脚本数组问题 - Google apps script array issues 在 Google Apps 脚本中返回一个数组 - Returning an array in Google Apps Script 如何使用 Google Apps 脚本遍历范围,获取第一个 Col 数据并继续迭代以形成 html 表? - How to iterate through a range, get 1st Col data once and continue iterating to form an html table, using Google Apps Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM