Vineethvarma Vegesna
Vineethvarma Vegesna

Reputation: 73

How to update a cell in google sheets using Google Apps Script

Currently, I'm using sheet.appendRow([name]) to add a row in google sheets, but I wanted to update only one cell every time a new data comes

Code:

function doGet(request){
    // Open Google Sheet using ID
    var sheet = SpreadsheetApp.openById("SHEET_ID");
    var result = {"status": "SUCCESS"};

    try{
        // Get all Parameters
        var name = request.parameter.name;

        // Append data on Google Sheet
        var rowData = sheet.appendRow([name]);  

    }catch(exc){
        // If error occurs, throw exception
        result = {"status": "FAILED", "message": exc};
    }

    // Return result
    return ContentService
    .createTextOutput(JSON.stringify(result))
    .setMimeType(ContentService.MimeType.JSON);  
}

Upvotes: 1

Views: 1866

Answers (2)

roma
roma

Reputation: 1580

You can use

sheet.getRange(sheet.getLastRow()+1,1).setValue(name);

Upvotes: 0

Vineethvarma Vegesna
Vineethvarma Vegesna

Reputation: 73

I found the solution

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var cell = sheet.getRange("B2");
cell.setValue(100);

Upvotes: 1

Related Questions