set cell value google script

func setCellValue(sheet *sheets.Service, spreadsheetID, sheetName, cell string, value interface{}) error {
    valueRange := sheets.ValueRange{
        Values: [][]interface{}{{value}},
    }
    _, err := sheet.Spreadsheets.Values.Update(spreadsheetID, sheetName+"!"+cell, &valueRange).
        ValueInputOption("RAW").
        Do()
    if err != nil {
        return err
    }
    return nil
}

Explanation:

  1. The function setCellValue takes the following parameters:
  2. sheet: Represents the Google Sheets service.
  3. spreadsheetID: ID of the spreadsheet where the cell exists.
  4. sheetName: Name of the sheet where the cell exists.
  5. cell: The specific cell to update (e.g., "A1", "B2").
  6. value: The value to set in the specified cell.

  7. valueRange is a variable of type sheets.ValueRange that holds the new value to be set. It's formatted as a two-dimensional slice (2D array) containing a single element (the value to set) within another array. This structure is required by the Sheets API for value updates.

  8. The sheet.Spreadsheets.Values.Update method updates the value in the specified cell within the spreadsheet. It takes the spreadsheetID, sheetName+"!"+cell (the specific cell in A1 notation), and the valueRange containing the new value.

  9. ValueInputOption("RAW") specifies that the input value should be treated as raw text and not be parsed or evaluated as a formula.

  10. Do() executes the update operation.

  11. The function returns an error if any issues occur during the update process; otherwise, it returns nil to indicate success.