Variable is changed in script without reason?

23 views
Skip to first unread message

Bill Smith

unread,
Oct 17, 2024, 2:05:29 AMOct 17
to Google Apps Script Community
Here is my code
  // Define some app variables
  var blkSrcRange = 'A2:C6';
  var grySrcRange = 'E1:G6';
  var whSrcRange = 'I1:I11';
  var tnkClean1StartRange = 'A4';
  var srcContents = '';
 

  // Populate report page
  var tnk1 = svcRvw.getRange('B9').getValue();
  var tnk2 = svcRvw.getRange('B10').getValue();
  var tnk3 = svcRvw.getRange('B11').getValue();
  var tnk4 = svcRvw.getRange('B12').getValue();
  var tnk5 = svcRvw.getRange('B13').getValue();
  var tnk6 = svcRvw.getRange('B14').getValue();
Logger.log(tnk6)  ; // at this point - Logger shows tnk6 = 'No'
  if (tnk1 = 'Yes') {
    srcContents = tnkMaster.getRange(blkSrcRange);
    tnkClean1StartRange = tnkRvwRpt.getRange(tnkClean1StartRange);
    srcContents.copyTo(tnkClean1StartRange);
   
  // Get the last row with content in the specified column
  var lastRow = tnkRvwRpt.getLastRow();
  var tnkClean1StartRange = ('A' + (lastRow+2));
 
  }else if (tnk2 = 'Yes'){
    srcContents = tnkMaster.getRange(blkSrcRange);
    tnkClean1StartRange = tnkRvwRpt.getRange(tnkClean1StartRange);
    srcContents.copyTo(tnkClean1StartRange);
    var lastRow = tnkRvwRpt.getLastRow();
    var tnkClean1StartRange = ('A' + (lastRow+2));
  }
  if (tnk3 = 'Yes'){
    srcContents = tnkMaster.getRange(grySrcRange);
    tnkClean1StartRange = tnkRvwRpt.getRange(tnkClean1StartRange);
    srcContents.copyTo(tnkClean1StartRange);
   
  // Get the last row with content in the specified column
  var lastRow = tnkRvwRpt.getLastRow();
  var tnkClean1StartRange = ('A' + (lastRow+2));
  }else if (tnk4 = 'Yes'){
    srcContents = tnkMaster.getRange(grySrcRange);
    tnkClean1StartRange = tnkRvwRpt.getRange(tnkClean1StartRange);
    srcContents.copyTo(tnkClean1StartRange);
    var lastRow = tnkRvwRpt.getLastRow();
    var tnkClean1StartRange = ('A' + (lastRow+2));
  }else if (tnk5 = 'Yes'){
    srcContents = tnkMaster.getRange(grySrcRange);
    tnkClean1StartRange = tnkRvwRpt.getRange(tnkClean1StartRange);
    srcContents.copyTo(tnkClean1StartRange);
    var lastRow = tnkRvwRpt.getLastRow();
    var tnkClean1StartRange = ('A' + (lastRow+2));
  }
  if (tnk6 = 'Yes') { //I have a breakpoint here - the debug panel shows
// the var tnk6 = 'No'
// considering the comparison - this next section should be bypassed
// it is not - in fact using the debug 'step into' link - the next line (below)
// is highlighted and the debug panel shows tnk6 = 'Yes'
    srcContents = tnkMaster.getRange(whSrcRange);
    tnkClean1StartRange = tnkRvwRpt.getRange(tnkClean1StartRange);
    srcContents.copyTo(tnkClean1StartRange);
    var lastRow = tnkRvwRpt.getLastRow();
    var tnkClean1StartRange = ('A' + (lastRow+2));
  }


Can anyone possibly offer an explanation or a workaround>

Thanks in advance...
Arden

Edward Wu

unread,
Oct 17, 2024, 12:19:24 PMOct 17
to google-apps-sc...@googlegroups.com
Single equals signs don't work for comparisons and will give you unexpected results.

You need to use double equal signs (or sometimes triple). (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness)

Change this:
if (tnk6 = 'Yes')

...to this...
if (tnk6 == 'Yes')


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/c0b7e802-7b3f-4c33-a3c3-d3e218806a6fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages