0

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

4
  • Can I ask you about "location 0"? Commented Oct 1, 2018 at 7:56
  • Location 0 as in the first sheet inside a spreadsheet that has at least two sheets. Commented Oct 1, 2018 at 12:39
  • Isn't it supposed to be sheets.DeleteSheetRequest and not sheets.DeleteSheet? .DeleteSheet is under Request not sheets Commented Oct 1, 2018 at 12:52
  • @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. Commented Oct 1, 2018 at 23:54

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.