I have just started writing JS for Google Apps Scripts to automate some of my Google Sheets. So my apologies in advance if there is an obvious error.
I have defined an object using constructor, when I try to make new objects of the same type giving the variable values in round brackets those values do seem to be taken.
Here's the code:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("First Sorting");
var negsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Negative List");
function pastesheet (lastrow, append, pasterow) {
this.lastrow = sheet.getLastRow();
this.append = 1;
this.pasterow = sheet.getRange(1, 1);
}
var neg = new pastesheet(negsheet.getLastRow(), 1, negsheet.getRange(1, 1));
From logger I observed that using the above code neg.lastrow actually refers to the last row of sheet "First Sorting" and not "Negative List" that I want it to refer to. I also observed that the following tweak to the last line of above code does what I want:
ver neg = new pastesheet();
neg.lastrow = negsheet.getLastRow();
neg.append = 1;
neg.pastrow = negsheet.getRange(1,1)
However, I would really like to know what am I doing wrong in the first instance? It is quite convenient to assign everything in one line and I plan to do this for quite a few sheets.
Your constructor is referencing
sheetwhich is not an argument and thus references the "First Sorting"sheetdefined in the global body.Did you intend to do something along these lines:
?