copy paste value - how can I consider 0 as a not null value ?

189 views
Skip to first unread message

Soukayna BEN MOH

unread,
Jul 8, 2022, 7:28:18 AM7/8/22
to Google Apps Script Community
Hello,
I wanted to copy and paste the last non-zero cell value each time
I have it in function and the goal is to copy and paste it in value
here is the script I used :


function copypastevalue() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var tabCAdaily = ss.getSheetByName("CA Daily");
  var valuesH = tabCAdaily.getRange("E2:E417").getValues();
  var lastlinenotemptynumberH = 0;
  while (valuesH[numLastLineNonEmptyH][0] != "" && valuesH[numLastLineNonEmptyH][0] != "0" ) {
    numLastLineNotEmptyH++;
    }
  var lastvalueH = tabCAdaily.getRange(numLastLineNotEmptyH+1,5).getValue();
   tabCAdaily.getRange(numberLastLineNotEmptyH+1,5).setValue(lastvalueH);

};


it works very well but it considers me 0 as null value also knowing that it should not be
how can i fix this problem please
Thanks 

Clark Lind

unread,
Jul 9, 2022, 1:02:17 PM7/9/22
to Google Apps Script Community
Two things that I see as potential issues. First, you have turned the zero into a string when you use quotes  != "0".. should just be != 0. 
Second, Typescript was invented because of this issue with javascript (apps script).. lol  So to be absolutely sure you are using a zero and not a 'falsey' value, use two equal signs:
 !== 0.
I think that is the main problem, you are returning a falsey value instead of an an actual zero.  In javascript, "" and null, and 0, and NaN, and UNDEFINED, and "0" all are considered 'false'. The last one, as in your function, is considered not a number (NaN).

Clark Lind

unread,
Jul 9, 2022, 1:03:51 PM7/9/22
to Google Apps Script Community
Reply all
Reply to author
Forward
0 new messages