我有一个带有更新对象变量的函数的对象。我也想将此函数用作构造函数,因此我可以在其中定义属性。如果我只定义此函数中的属性,则其他函数无法实现更改。 我的意思是,this.property返回undefined。
该对象如下所示:
function LastRow_(sheet)
{
var lastCol,firstRowVals,rowNumber,headers;
this.rowValues;
updateObject();
// Logger.log(headers);
/*Returns the value of a specific column within last row.
It accepts header name or column letter in A1 notation*/
this.getColValue=function(column)
{
updateObject();
Logger.log(this.rowValues);
var index=getColNumber(column);
if(index>-1) return this.rowValues[index];
Logger.log("Can't find header, trying with: "+column+rowNumber);
return sheet.getRange(column+rowNumber).getValue();
};
this.setColValue=function(column,value)
{
updateObject();
};
function getColNumber(colName){ return headers.indexOf( colName.toLowerCase() ); }
/*This function is to be sure that you are allways working with the last row
and an updated version of the headers*/
function updateObject()
{
if(lastCol!==sheet.getLastColumn())
{
Logger.log("Updating row Headers");
lastCol=sheet.getLastColumn();
firstRowVals=sheet.getRange(1,1,1,lastCol).getValues()[0];
headers=[];
for(var i=0;i<firstRowVals.length;i++)
headers.push(firstRowVals[i].toLowerCase());
Logger.log(headers);
}
if(rowNumber!==sheet.getLastRow())
{
Logger.log("Updating row Values");
rowNumber=sheet.getLastRow();
this.rowValues=sheet.getRange(rowNumber,1, 1, lastCol).getValues()[0];
Logger.log(this.rowValues);
}
};
}
对对象方法的任何调用都会产生错误
function test() {/*valueLastRow(SpreadsheetApp.getActiveSpreadsheet().getSheetByName('workedDays'),"C",5)*/
var LastRow=new LastRow_(SpreadsheetApp.getActiveSpreadsheet().getSheetByName('workedDays'));
Logger.log(LastRow.getColValue("D"));
Logger.log(LastRow.getColValue("sede"));
}
问题是this.rowNumber未定义。
提前致谢。