简体   繁体   中英

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. 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 :

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).

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.

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(). 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).

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 white space is found you could clear() the range or change the array and use setValues() to place it back in the column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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