0
votes

I'm not quite understanding how the google sheets api for go works.

What I want to do is delete sheet at location 0 from a spreadsheet. Here is the code snippit for the request that doesn't work right now.

    rb2 := &sheets.BatchUpdateSpreadsheetRequest{
            Requests: requests,
    }

    resp2, err := srv.Spreadsheets.BatchUpdate(destinationSpreadsheetId, rb3).Do()

What I thought I'd do is create request before making the request body on line 1 above.

    ds := &sheets.DeleteSheetRequest{
            SheetId: int64(0),
    }
    deleteSheet := &sheets.DeleteSheet{
        DeleteSheetRequest: ds,
    }    
    requests := []*sheets.Request{
        DeleteSheet: deleteSheet,
    }

If I try to build it, the compiler will error,

sheets\sheets.go:118:19: undefined: sheets.DeleteSheet
sheets\sheets.go:123:4: undefined: DeleteSheet

I was trying to follow the sheets manual, https://godoc.org/google.golang.org/api/sheets/v4#BatchUpdateSpreadsheetRequest

1
Can I ask you about "location 0"?Tanaike
Location 0 as in the first sheet inside a spreadsheet that has at least two sheets.Hai
Isn't it supposed to be sheets.DeleteSheetRequest and not sheets.DeleteSheet? .DeleteSheet is under Request not sheetstehhowch
@Hai Thank you for replying. If you have already known the sheet ID you want to remove, you can remove the sheet using it. If you don't have the sheet ID, at first, please retrieve the sheet ID of the first index in the Spreadsheet using spreadsheets.get.Tanaike

1 Answers

0
votes

The fix:

    deleteSheetRequest := &sheets.DeleteSheetRequest{
        SheetId: 0,
    }

    requests := []*sheets.Request{
        {DeleteSheet: deleteSheetRequest},
    }

    rb3 := &sheets.BatchUpdateSpreadsheetRequest{
            Requests: requests,

            // TODO: Add desired fields of the request body.
    }

    resp2, err := srv.Spreadsheets.BatchUpdate(destinationSpreadsheetId, rb3).Do()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(resp2)

@tehhowch, thanks for the help. I fixed that and then I eventually realized after looking at the code that the []*sheets.Request was missing array brackets. Duh.