Gray_code_element

Wednesday, May 21, 2025

Google Sheet Update from HTTP POST

 The following code will update a Google sheet:

function doGet(e) {
    // This function handles GET requests (for simplicity in this example)
 
    // Get parameters from the URL
    var sheetName = e.parameter.sheetName;
    var row = parseInt(e.parameter.row);
    var col = parseInt(e.parameter.col);
    var value = e.parameter.value;
 
    // Basic error handling
    if (!sheetName || isNaN(row) || isNaN(col) || value == = undefined) {
        return ContentService.createTextOutput("Error: Missing parameters (sheetName, row, col, value)").setMimeType(ContentService.MimeType.TEXT);
    }
 
    try {
        // Get the spreadsheet and the specific sheet
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var sheet = ss.getSheetByName(sheetName);
 
        if (!sheet) {
            return ContentService.createTextOutput("Error: Sheet not found: " + sheetName).setMimeType(ContentService.MimeType.TEXT);
        }
 
        // Update the specified cell (row and column are 1-based)
        sheet.getRange(row, col).setValue(value);
 
        return ContentService.createTextOutput("Cell updated successfully! Sheet: " + sheetName + ", Row: " + row + ", Col: " + col + ", Value: " + value).setMimeType(ContentService.MimeType.TEXT);
 
    }
    catch (error) {
        return ContentService.createTextOutput("Error: " + error).setMimeType(ContentService.MimeType.TEXT);
    }
}

3 comments:

  1. Never thought updating a Google Sheet could be this simple with just a GET request. Kinda wild that you can pass row, col, and value straight in the URL. Gonna try this out for some quick tracking at work. Also, found some other neat tools like YouTube Vanced that make life easier when tinkering with small scripts.

    ReplyDelete
  2. Using Google Apps Script to update specific Google Sheets cells through URL parameters is a practical approach to lightweight automation. The example demonstrates how to identify a worksheet, validate input parameters, update a cell, and return a response indicating success or failure. For production use, adding authentication and input validation is also important to protect spreadsheet data.

    Understanding HTTP requests, query parameters, response handling, and endpoint security helps developers integrate applications with Google Workspace. A REST API Course can help learners understand API communication and build reliable integrations between different software systems.

    ReplyDelete
  3. JavaScript is particularly relevant to Google Apps Script because it provides the programming foundation for handling request parameters and automating spreadsheet operations. Through Javascript Online Training in India, learners can strengthen their programming skills and apply them to practical automation and web development tasks.

    ReplyDelete

y bar

y + hold Alt, type 0773