简体   繁体   English

Google Sheets / Javascript -> 循环遍历数组并将值设置为单元格

[英]Google Sheets / Javascript -> Loop through array and set the values to the cells

I am new to programming and I have such a simple question but I struggle to find the answer.我是编程新手,我有一个如此简单的问题,但我很难找到答案。 I would like to dynamically overwrite cells from A1 on until the lenght of the array.我想从 A1 开始动态覆盖单元格,直到数组的长度。 This is the second for loop I am struggling with.这是我正在努力解决的第二个 for 循环。 The combination of ("A" + ii) for the range doesnt look "professional" :-) Thanks for your help.该系列的 ("A" + ii) 组合看起来并不“专业”:-) 感谢您的帮助。

function selectmyagency() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var agencynames = ss.getRange("B8:B42").getValues();
  var myagency = ss.getRange("C3").getValue();

  var arrayLength = agencynames.length;

  for (var i = 0; i < arrayLength; i++) {
    if(agencynames[i] == myagency){
      //doNothing
    } else {
      agencynames[i] = ".";
    }//endif 
  }//endfor

  //overwrite Cell in Spreadsheet
  for (var ii = 0; ii < agencynames.length; ii++) {
    SpreadsheetApp.getActiveSheet().getRange("A"+ii).setValue(agencynames[ii]);
    //SpreadsheetApp.getActiveSheet().getRange("A9").setValue(agencynames[ii]);
    //SpreadsheetApp.getActiveSheet().getRange("A10").setValue(agencynames[ii]);
  }
}//endfunction

Instead of looping through the array and setting the ranges value one cell at a time, you can do this in a batch operation like so: ss.getRange("B8:B42").setValues(agencynames);您可以像这样在批处理操作中执行此操作,而不是遍历数组并一次设置一个单元格的范围值: ss.getRange("B8:B42").setValues(agencynames);

Do this after modifying the agencynames array, this will set all the values of that range to match your array as long as the array and the range are the same size.在修改机构agencynames数组后执行此操作,只要数组和范围大小相同,就会将该范围的所有值设置为与您的数组匹配。 It's generally discouraged to make calls to a service in a loop when you can use a batch operation, for performances and readabilities sake.当您可以使用批处理操作时,通常不鼓励在循环中调用服务,以提高性能和可读性。

For more information, refer to the Apps Script Best Practices有关更多信息,请参阅Apps 脚本最佳实践

Edit: Your modified code:编辑:您修改后的代码:

function selectmyagency() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var agencynames = ss.getRange("B8:B42").getValues();
  var myagency = ss.getRange("C3").getValue();

  var arrayLength = agencynames.length;

  for (var i = 0; i < arrayLength; i++) {
    if(agencynames[i] == myagency){
      //doNothing
    } else {
      agencynames[i] = ".";
    }//endif 
  }//endfor

  //overwrite Cell in Spreadsheet
  ss.getRange("B8:B42").setValues(agencynames);
}//endfunction

A couple more pointers:还有一些提示:

  • There is no need to set an array length variable if you are only modifying the elements themselves and not the array.如果您只是修改元素本身而不是数组,则无需设置数组长度变量。
  • When you use getValues() you are getting an array of arrays not an array of values, even if it is only a single column.当您使用getValues()您得到的是数组数组而不是值数组,即使它只是一列。 ie. IE。 [["value"],["value"],["value"]] instead of ["value","value","value"] . [["value"],["value"],["value"]]而不是["value","value","value"] When setting or getting the values of this array, you probably want to use agencynames[i][0] instead of agencynames[i]设置或获取此数组的值时,您可能希望使用机构agencynames[i][0]而不是机构agencynames[i]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google表格脚本-循环不同单元格中的值 - Google Sheets Script - Loop values in different cells Google表格中的JavaScript“ for”循环单元格数据比较 - Javascript “for” loop of cells data comparison in Google Sheets 如何遍历值列表并将输出导出到不同的谷歌工作表 - How to loop through a list of values and export the outputs to a different google sheets Google表格 - 通过表格整合数据 - Google Sheets - Loop through Sheets consolidating data 需要使用Javascript在Google表格中生成4x或2x报表。 1数据集。 尝试不遍历数据4次 - Need to generate 4x or 2x reports in Google Sheets using Javascript. 1 Data set. Trying not to loop through data 4 times javascript 循环遍历元素的动态数组,获取值然后使用值作为间隔来设置计时器倒计时 - javascript loop through dynamic array of elements, get values then use values as interval to set timer countdown Google Sheets Script - 循环遍历所有工作表,并将某一列的所有值添加到数组中,然后计算唯一值 - Google Sheets Script - Looping through all sheets, and adding all values of a certain column into array to then count unique values 在Javascript中一次按一组4个元素循环遍历数组? - Loop through an array by set of 4 elements at a time in Javascript? 通过数组循环将插入值插入其中 - Loop through array an insert values into it javascript JavaScript回调通过循环推送值的数组 - JavaScript callback an array with values pushed through a loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM