witham
witham

Reputation: 149

Inserted formula not doing anything

I have put this little script together to insert an if(vlookup) formula into a spreadsheet but even though it is running it isn't inserting anything into the spreadsheet. Can anyone suggest what I have missed off or overlooked please?

Or is there an easier way to do this with an array? I'm still fairly new so not sure how I would execute that successfully.

    function idSiteCoordinator(){

    var SS = SpreadsheetApp.getActiveSpreadsheet();
    var shLog = SS.getSheetByName("LOG");

//declare  how many rows to interrogate (to the last row)
    var lastRow = shLog.getLastRow();
    var startRow = 2;

      for (var i=startRow; i<lastRow+1; i++)

    {

        //Grab the values for LocationCodes
        var siteCo = shLog.getRange([i],[12]).getValue();

        //Enter formula
        if(siteCo = ""){
        siteCo.setFormula('=if(E'+[i]+'="08 - Maintenance Request System",LocationCodes!$E$33,vlookup(D'+[i]+',LocationCodes!$D$3:$E$32,2,false))');
        }
    }
}

Upvotes: 0

Views: 297

Answers (1)

Rivero
Rivero

Reputation: 914

You cannot apply the method setFormula to the value, but to the range. Change this on your method:

    var siteCo = shLog.getRange([i],[12]);

    if(siteCo.getValue() == ""){
    siteCo.setFormula('=if(E'+[i]+'="08 - Maintenance Request System",LocationCodes!$E$33,vlookup(D'+[i]+',LocationCodes!$D$3:$E$32,2,false))');
    }

Upvotes: 1

Related Questions