0

I am facing the following problem, when I am trying to work with an object in javascript I have troubles assigning values inside of the object. Here especially with this.sItem[i] and oInvoice.sItem[i] which are always undefined.

var oInvoice = {
sID: "",
sDate: "",
iNumberOfItems: oSpreadSheetApp.hInvoiceSheet.getRange(SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell().getRow(), 2, 1, 1).getValues()[0],
sItem: "",
iRate: "",
iWork: "",
iCalculatedAmount: "",  
sBody: "",
sComment: "",

/* Get the actual data inside range */ 
/* Fetch values for each row in the Range. */
mProcessInvoice: function () {
    var rDataRange = oSpreadSheetApp.hInvoiceSheet.getRange(SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell().getRow(), 1, 1, 11);
    var dData = rDataRange.getValues();
    var i = 0;

    /* Read values */ 
    for (i in dData) {
        var rCurrentRow = dData[i]; /* Current row contains values from spreadsheet */

        /* Both this.sItem[i] and oInvoice.sItem[i] are empty */ 
        this.sItem[i] = rCurrentRow[4];
        oInvoice.sItem[i] = rCurrentRow[4];

    }       
}
};

Logger.log(oInvoice.sItem[1]);

I am pretty sure that I am not getting a basic concept here, but gladly look forward to be enlightened.

1 Answers1

0

Couple of problems

  1. sItem is declared as an string, declare it as an array
  2. Use the push method instead of [i], is saffer.
  3. "this" will reference the functions running the code not the object. Use prototype.functionName = function () {} to declare method, or do not use this, or use Function#call (not recommended, but just telling you)

If by any chance you want to concat string just use the += operator

var a = '';
a += 'Hello'
console.log(a); //"Hello"
Nickydonna
  • 802
  • 1
  • 5
  • 18