简体   繁体   English

GAS:如何修改这段代码以仅更改为空的单元格值?

[英]GAS: how to adapt this piece of code to change cell value only if it's empty?

I write a function which extract some needed information from a raw text in a spreadsheet and put it in other column. 我编写了一个函数,该函数从电子表格的原始文本中提取一些所需的信息,并将其放在其他列中。 But I'd like to use this function only when that column is empty (because, if it's not empty is because its value was manually updated). 但我只想在该列为空时使用此函数(因为如果不为空,是因为其值是手动更新的)。 I try to use the code bellow, but I figure out that s.getRange(2, 8, output.length, 1).getValue() will not give tell me if a given cell is empty or not. 我尝试使用下面的代码,但是我发现s.getRange(2, 8, output.length, 1).getValue()不会告诉我给定的单元格是否为空。 So, given the code bellow, how could I achieve that? 那么,鉴于下面的代码,我该如何实现?

var output = [];
   for (var i = 0; i < input.length; i++) {
     output.push([ findDate(input[i][0], 'myKeyWord:', -4, -3) ]);
     Logger.log("s.getRange(2, 8, output.length, 1).getValue() = " + s.getRange(2, 8, output.length, 1).getValue())
     if(s.getRange(2, 8, output.length, 1).getValue()){
        s.getRange(2, 8, output.length, 1).setValues(output); // 8 is col H
     }

If I understood correctly your use case, you could use a condition like below, it will check if there is a value in the cell (and also prevent the accidental presence of any space in the cell (some people have the bad habit to put white spaces in cells to delete things instead of clearing the cell) 如果我正确理解了您的用例,则可以使用以下条件,它将检查单元格中是否有值(并且还可以防止单元格中任何空间的意外出现(有些人习惯于将白色单元格中的空格以删除内容而不是清除单元格)

 if(s.getRange(2, 8, output.length, 1).getValue().toString().replace(/ /g,'').length > 0){
 ...
 }

edit : following @sscholefield comment : 编辑:以下@sscholefield评论:

I was indeed hesitating when answering this question , wondering wether you had to check the column or a single cell... since you where talking about manually editing a cell I took the option of a single cell but it appears that I might have been wrong... 当回答这个问题时,我确实很犹豫,想知道您是否必须检查列或单个单元格...因为在谈论手动编辑单元格的地方,我选择了单个单元格,但看来我可能错了...

In case you want to check if the whole column is empty you could change the code and replace getValue() with getValues() ... This will of curse not be sufficient in the comparison, the idea in that case would be to compare the array length with the length of the 'flattened' array, this latter being something like ",,," if empty (length=3) and the array length for the same example would be [[],[],[],[]] (length = 4). 如果你想检查整列是空的,你可以修改代码和替换getValue()getValues() ...诅咒这会不会是在比较充足,在这种情况下,想法是比较数组长度与“展平”数组的长度,如果为空(长度= 3),则后者类似于",,," ,并且同一示例的数组长度为[[],[],[],[]] (长度= 4)。

As you can see we have a length difference that we can catch. 如您所见,我们有一个可以捕捉的长度差。

Please tell us if this can solve the issue. 请告诉我们这是否可以解决问题。

I'd recommend to use the logger to see exactly what you are trying to compare and decide accordingly. 我建议使用记录器来准确查看您要比较的内容并做出相应的决定。

note : btw, thanks for your kind words, writing a book is something I'm thinking about seriously but for now it's still a project and should remain confidential (ha ha...;-) what?... we're on the internet ? 注意: 顺便说一句,谢谢您的客气,写书是我正在认真考虑的事情,但是现在它仍然是一个项目,应该保密(哈哈... ;-)什么?互联网 ? OMG ! 我的天啊 ! didn't realize...) 没意识到...)

I cannot comment so I need to write this here, however it is more of an observation than an answer, and comes at it from a slightly different angle to Serge. 我无法发表评论,因此我需要在这里写下,但是它更多的是观察,而不是答案,并且与Serge的角度略有不同。

Depending on the value of 'output.length' you are acquiring a multi-row array but you are only returning a single value from the top left cell of that range by using getValue(), not getValues(). 根据'output.length'的值,您要获取一个多行数组,但是您使用getValue()而不是getValues()仅从该范围的左上角单元格返回单个值。 Is this what you were expecting? 这是您所期望的吗?

If you need to check if the entire column is empty you may be better off using getValues() for the column to create an array and then checking the array length equals zero (remembering to -1 for headers). 如果需要检查整个列是否为空,最好使用getValues()对该列创建一个数组,然后检查该数组的长度是否为零(对于标题,请记住为-1)。

Unfortunately this would mean a two stage check if(array.length - 1 != 0) as you would have to iterate through to check if array[i] == ' ' (white space). 不幸的是,这将意味着两阶段检查if(array.length-1!= 0),因为您必须迭代检查是否array [i] ==''(空白)。 If white space is found you could clear() the range or change the array and use setValues() to place it back in the column. 如果找到空格,则可以清除()范围或更改数组,然后使用setValues()将其放回到列中。

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

相关问题 找不到匹配项(GAS)时如何在下一个空单元格中插入字符串? - How to insert the string in the next empty cell when found no match (GAS)? JavaScript,如何只运行一次代码? - JavaScript, how to run a piece of code only once? 查找与工作表单元格中的值匹配的 Google 表格并执行一段代码 - Find Google Sheets that matches a value in a cell of a sheet and execute a piece of code 这段代码如何运行变化检测angular? - How this piece of code running change detection angular? GAS Google Script - 如何复制值并粘贴到另一个单元格 - GAS Google Script - How copy value and paste in another Cell GAS 根据单元格值获得单元格 +1? - GAS get cell +1 based on cell Value? 如何仅更改 html 表格中最小单元格的颜色 - How to change only minimum cell's color in html tables GAS扫描文件夹中的所有电子表格,并更改特定的单元格值和工作表名称? - GAS Scan all spreadsheets in folder and change a specific cell value and sheet name? 无法使用GAS向单元格正确添加价值 - Unable to properly add value to a cell using GAS GAS/Javascript:如何将值设置为仅工作表底部的空行 - GAS/Javascript: How do I set values to only empty rows at the bottom of the sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM