简体   繁体   中英

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);  
}

I found the solution

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

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

You can use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM