Home >Backend Development >Golang >How to Write Data to a Google Spreadsheet Using Go and Sheets API V4?

How to Write Data to a Google Spreadsheet Using Go and Sheets API V4?

Linda Hamilton
Linda HamiltonOriginal
2024-11-05 14:42:02997browse

How to Write Data to a Google Spreadsheet Using Go and Sheets API V4?

Writing Data to Google Sheets Using Go and Sheets API V4

Many developers find the Go library for the Sheets API daunting, especially when it comes to writing data to a spreadsheet. This article will provide a clear and concise solution to this task, extending the capabilities of the popular Sheets API Quickstart Guide.

To write data to a Google spreadsheet, you can leverage the Write() function. First, you'll need to create a ValueRange object to represent the data you want to write. This object can contain a list of rows, where each row is a list of strings or numeric values.

Next, specify the range of cells to which you want to write using writeRange. For example, A1 indicates writing to the top-left cell. Finally, use the Update() method of the Sheets service to perform the write operation. Set the ValueInputOption to RAW to preserve data formats.

Here is an example Write() function that writes a three-column table:

<code class="go">import (
    "context"
    "fmt"
    "io/ioutil"
    "log"

    "github.com/GoogleCloudPlatform/golang-samples/common/auth"
    sheets "google.golang.org/api/sheets/v4"
)

func write() {
    ctx := context.Background()
    b, err := ioutil.ReadFile("./Google_Sheets_API_Quickstart/client_secret.json")
    if err != nil {
        log.Fatalf("Unable to read client secret file: %v", err)
    }

    config, err := google.ConfigFromJSON(b, "https://www.googleapis.com/auth/spreadsheets")
    if err != nil {
        log.Fatalf("Unable to parse client secret file to config: %v", err)
    }
    client := auth.GetClient(ctx, config)

    srv, err := sheets.New(client)
    if err != nil {
        log.Fatalf("Unable to retrieve Sheets Client %v", err)
    }

    spreadsheetId := "YOUR SPREADSHEET ID"
    writeRange := "A1"

    var vr sheets.ValueRange
    myval := []interface{}{"One", "Two", "Three"}
    vr.Values = append(vr.Values, myval)

    _, err = srv.Spreadsheets.Values.Update(spreadsheetId, writeRange, &vr).ValueInputOption("RAW").Do()
    if err != nil {
        log.Fatalf("Unable to retrieve data from sheet. %v", err)
    }
    fmt.Println("Data written to spreadsheet.")
}</code>

Incorporate this Write() function into your Go program to effortlessly write data to any cell range in your Google spreadsheet.

The above is the detailed content of How to Write Data to a Google Spreadsheet Using Go and Sheets API V4?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn