简体   繁体   English

Google Apps 编写更好的方法来获取唯一值

[英]Google Apps Script Better Way to Get Unique Values

I have working code that takes data from two non-adjacent columns in a Google Spreadsheet, looks for unique values in the first column, and if unique creates a new array with the unique value from the first column and corresponding value in the second column.我的工作代码从 Google 电子表格中的两个不相邻列中获取数据,在第一列中查找唯一值,如果唯一,则创建一个新数组,其中包含第一列中的唯一值和第二列中的相应值。 The problem is, the data I am using is already somewhat long (413 rows) and will only get longer over time.问题是,我使用的数据已经有点长(413 行),并且随着时间的推移只会变得更长。 It takes about 1-2 minutes for the code to run through it.代码运行大约需要 1-2 分钟。 I've been looking for a shorter way to do this and I've come across the filter() and map() array functions which are supposedly faster than a for loop but I can't get them implemented correctly.我一直在寻找一种更短的方法来执行此操作,并且遇到了 filter() 和 map() 数组函数,它们据说比 for 循环更快,但我无法正确实现它们。 Any help with these or a faster method would be greatly appreciated.任何有关这些或更快方法的帮助将不胜感激。 The code I have right now is below.我现在拥有的代码如下。

function getkhanassignments(rows) {

  var assignmentsraw = [];
  var temparray = [];
  var previousassignment = datasheet.getRange(50,1).getValue();
  for(i=0, j=0;i<rows-1;i++) {
    if(datasheet.getRange(50+i,1).getValue() != previousassignment) {   
      previousassignment = datasheet.getRange(50+i,1).getValue();
      assignmentsraw[j] = new Array(2);
      assignmentsraw[j][0] = datasheet.getRange(50+i,1).getValue();
      assignmentsraw[j][1] = datasheet.getRange(50+i,8).getValue();
      j++;
    }
  }
  Logger.log(assignmentsraw);
  return assignmentsraw;
}

The answers I've found elsewhere involve just getting unique values from a 1d array whereas I need unique values from a 1d combine with corresponding values from another 1d array.我在其他地方找到的答案只涉及从一维数组中获取唯一值,而我需要来自一维数组的唯一值与来自另一个一维数组的相应值相结合。 The output should be a 2d array with unique values from the first column and their corresponding values in the second column. output 应该是一个二维数组,第一列的唯一值和第二列的对应值。

Solution:解决方案:

The best practice of looping through ranges in Google Apps Script is to dump the range values into a 2D array, loop through that array, and then return the output array back to Google Sheets.在 Google Apps 脚本中循环遍历范围的最佳做法是将范围值转储到二维数组中,循环遍历该数组,然后将 output 数组返回给 Google 表格。

This way, there would be no calls to Sheets API inside loops.这样,就不会在循环内调用 Sheets API。

Sample Code:示例代码:

function getkhanassignments(rows) {

  var assignmentsraw = [];

  var table1 = datasheet.getRange(50,1,rows).getValues();
  var table2 = datasheet.getRange(50,8,rows).getValues();

  var previousassignment = table1[0][0];
  assignmentsraw.push([table1[0][0],table2[0][0]]);
  for(i=0; i<rows; i++) {
    if (table1[i][0] != previousassignment) {
      assignmentsraw.push([table1[i][0],table2[i][0]]);
      previousassignment = table1[i][0];
    }
  }
  Logger.log(assignmentsraw);
  return assignmentsraw;
}

References:参考:

Class Range Class 范围

push() 推()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM