javascript 检查/引用空数组值(Google App Script)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32343116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking/ Referencing Empty Array Values (Google App Script)
提问by MasterCrander
I have a Google Spreadsheet that does some simple cell referencing and math for monthly travel for work. I have a function that reads the values in column A (which are attained by the WEEKDAY() formula on the sheet. It blacks out the whole row if it is a Saturday(value 5) or Sunday(value 6). I need it to format the weekdays a certain way (values 0-4) and, only if the date cell in that row is blank, to reformat that row back to the standard blank. How do I search through an array (you can see how I created it) and find an empty value in that array? I will copy-paste an example log of the array values (notice the periodic [])
我有一个 Google 电子表格,它可以为每月的工作旅行做一些简单的单元格引用和数学计算。我有一个函数可以读取 A 列中的值(通过工作表上的 WEEKDAY() 公式获得。如果是星期六(值 5)或星期日(值 6),它会将整行涂黑。我需要它以某种方式格式化工作日(值 0-4),并且仅当该行中的日期单元格为空白时,才将该行重新格式化为标准空白。如何搜索数组(您可以看到我是如何创建的)它)并在该数组中找到一个空值?我将复制粘贴数组值的示例日志(注意定期 [])
function weekendClean() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getActiveSheet()
var range = sheet.getRange("A3:A33")
var value = range.getValues()
var sheetCheckRange = sheet.getRange(2,2)
var sheetCheck = sheetCheckRange.getValue()
if (sheetCheck == "Date") {
for (i = 0; i < value.length; i++) {
if (value[i] == 5 || value[i] == 6) {
var row = sheet.getRange((i + 3), 1, 1, 11)
var cell = sheet.getRange((i + 3), 3)
row.setBackground("Black")
row.setBorder(true, null, true, null, null, null)
//top, left, bottom, right, vertical, horizontal
cell.setValue("W")
} if (value[i] == 0 || value[i] == 1 || value[i] == 2 || value[i] == 3 || value[i] == 4) {
var section1 = sheet.getRange((i+3), 2, 1, 5)
var section2 = sheet.getRange((i+3), 8, 1, 3)
section1.setBackground("white")
section2.setBackground("white")
section1.setBorder(false, null, false, null, null, false)
section2.setBorder(false, null, false, null, null, false)
}
}
}
}
Also, feel free to clean up that code with explanation (especially for the bit that reads what to do for the weekdays)
另外,请随时用解释清理该代码(特别是对于读取工作日要做的事情的部分)
[15-09-01 17:54:04:051 PDT] [[1.0], [2.0], [], [4.0], [5.0], [6.0],
[0.0], [1.0], [], [3.0], [4.0], [5.0], [], [], [1.0], [], [3.0],
[4.0], [5.0], [6.0], [0.0], [1.0], [2.0], [3.0], [], [5.0], [6.0],
[0.0], [1.0], [2.0], []]
回答by Etienne de Villers
This is how you would retrieve an empty array in your situation :
这是在您的情况下检索空数组的方式:
var x = [[1.0], [2.0], [], [4.0], [5.0], [6.0], [0.0], [1.0], [], [3.0], [4.0], [5.0], [], [], [1.0], [], [3.0], [4.0], [5.0], [6.0], [0.0], [1.0], [2.0], [3.0], [], [5.0], [6.0], [0.0], [1.0], [2.0], []];
function myFunction(x) {
for (var i = 0; i < x.length; i ++) {
if(x[i].length == 0){
// array is empty
Logger.log('empty');
} else {
//array not empty
Logger.log('not empty');
}
}
}
I also noticed that the ";" is missing from your statements. Your script runs as it is ?
我还注意到“;” 您的陈述中缺少。您的脚本按原样运行?
Cheers
干杯
回答by MasterCrander
I worked around the issue of finding which spot in the array was empty and rather set up a variable to reference each cell in the loop and check if it itself was blank. If it saw that it was, it performed the code block. Example below.
我解决了查找数组中哪个位置为空的问题,而是设置一个变量来引用循环中的每个单元格并检查它本身是否为空。如果它看到它是,它执行代码块。下面举例。
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getActiveSheet()
var range = sheet.getRange("A3:A33")
var value = range.getValues()
var sheetCheckRange = sheet.getRange(2,2)
var sheetCheck = sheetCheckRange.getValue()
if (sheetCheck == "Date") {
for (i = 0; i < value.length; i++) {
var row = sheet.getRange((i + 3), 1, 1, 11)
var sessionCell = sheet.getRange((i + 3), 3)
var dateCell = sheet.getRange((i+3), 2)
var section1 = sheet.getRange((i+3), 2, 1, 5)
var section2 = sheet.getRange((i+3), 8, 1, 3)
if (value[i] == 5 || value[i] == 6) {
row.setBackground("Black")
row.setBorder(true, null, true, null, null, null)
//top, left, bottom, right, vertical, horizontal
sessionCell.setValue("W")
} if (value[i] == 0 || value[i] == 1 || value[i] == 2 || value[i] == 3 || value[i] == 4) {
section1.setBackground("white")
section2.setBackground("white")
section1.setBorder(false, null, false, null, null, false)
section2.setBorder(false, null, false, null, null, false)
} if (dateCell.isBlank()) {
section1.setBackground("white")
section2.setBackground("white")
section1.setBorder(false, null, false, null, null, false)
section2.setBorder(false, null, false, null, null, false)
sessionCell.setValue("")
}
}
}
}
I also worked around needing a separate trigger by renaming the function to function onEdit() This also makes it much easier to copy the spreadsheet for sharing within the company. With the function named 'onEdit()', there's no need for authorization and setup of triggers by each user.
我还通过将函数重命名为 onEdit() 函数来解决需要一个单独的触发器这也使得复制电子表格以在公司内共享变得更加容易。使用名为“onEdit()”的函数,每个用户无需授权和设置触发器。
回答by Israel A G
Use a comparison to null
使用与null的比较
var x = [[1.0], [2.0], [], [4.0], [5.0], [6.0], [0.0], [1.0], [], [3.0], [4.0], [5.0], [], [], [1.0], [], [3.0], [4.0], [5.0], [6.0], [0.0], [1.0], [2.0], [3.0], [], [5.0], [6.0], [0.0], [1.0], [2.0], []];
function myFunction(x) {
for (var i = 0; i < x.length; i ++) {
if(x[i] == null){
// array is empty
Logger.log('empty');
} else {
//array not empty
Logger.log('not empty');
}
}
}
回答by Jens Astrup
The ArrayLib librarywould be able to do this for you, it makes the process of dealing with 2D arrays much simpler. For this, you'd want the IndexOf function, and use -1 to search through all columns.
该ArrayLib库将能够为你做到这一点,使得处理二维数组简单得多的过程。为此,您需要 IndexOf 函数,并使用 -1 来搜索所有列。