首先,这里是JS / Apps Script的新手。 :)
我正在尝试用Apps Script替换一些现有的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]。我很确定所有数组的编码都正确,可以按升序排序以加快索引编制速度。
谢谢!
大家都可以提供任何帮助/建议,我们将不胜感激。
最佳答案
您引用的是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 。