Apps脚本的索引(Match())是否等效?

时间:2018-01-14 18:01:06

标签: javascript google-apps-script google-sheets

首先关闭:这里有JS / Apps脚本新手。 :)

我试图用Apps脚本替换一些Google表格公式,并且想知道我将如何编写以下if语句:

var EMAIL_SENT = "EMAIL_SENT";

function onFormSubmitTest(e) {
    //Open spreadsheet based on URL
    var ss = SpreadsheetApp.openByUrl('sheetnamehere');
    //Set as Active
    SpreadsheetApp.setActiveSpreadsheet(ss);
    //Set Tabs as Variables
    var Rsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Emailer");
    var Lsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Unique Databases");

    //----Set last Rows as Variables----
    // Gets Values from different sections, filters arrays to non-null values off of [0] index
    var formResponses = Rsheet.getRange("N3:N").getValues().filter(function(row) {return row[0]}).sort();
    var databaseNames = Lsheet.getRange("A2:B").getValues().filter(function(row) {return row[0]}).sort();
    var developerNames = Lsheet.getRange("F2:F").getValues().filter(function(row) {return row[0]}).sort();

    // Attempt finding current process owner's e-mail address

    // Emailer Rows to Process
    var startRow = 3; // First row of data to process.  This is required because of various formatting messed up the opportunity for Last Row being the only row in the dataRange.

    // Number of rows to process
    var numRows = formResponses.length;

    // Fetch the range of cells
    var dataRange = Rsheet.getRange(startRow, 1, numRows, 30);

    // Fetch values for each row in the Range.
    var data = dataRange.getValues();

我试图加载到变量中的语句是var data [16] = var databaseNames [0]的值,返回var databaseNames [1]。我非常确定所有数组都已正确编码,以便按升序排序,以便更快地编制索引。

谢谢!

非常感谢您提供的所有帮助/建议。

2 个答案:

答案 0 :(得分:1)

我还不完全清楚,但我认为这可能就是你要找的东西。在你的问题中,也许编辑它给出了数据结构和性质的具体例子。

var result = "";

var data = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32];
var databaseNames  = [16,19];

var a = data[16]; // 16
var b = databaseNames[0]; //16

if(a === b){
   result = a //16
}

答案 1 :(得分:0)

您指的是data [16]和databaseNames [0],但请注意range.getValues()返回一个二维数组。 databaseNames [0]将是dataBaseNames []中的第一个数组元素,相当于该范围中的第一行。

我不清楚您是想要返回数组还是单个值。如果你只想弄清楚当databaseNames [i] [0] = x时如何在databaseNames [i] [1]中返回值,那么这样的事情就应该这样做:

findMatch = function(x) {
  var returnValue;
  // assuming databaseNames exists in parent or global scope...
  databaseNames.forEach(function(row){
    if (row[0] === x) returnValue = row[1];
  });
  return returnValue;
}

如果要返回与data [i] [16]匹配的值数组,可以执行var newArray = data.map(findMatch),并修改函数以将比较值设置为x [16]而不是x。