0

I'm trying to add a footer to my docs in Go. Here is my code.

    createFooterReq := &docs.Request{
        CreateFooter: &docs.CreateFooterRequest{
            SectionBreakLocation: &docs.Location{},
            Type:                 "DEFAULT_FOOTER",
        },
    }

    // Insert text into the footer
    footerText := "This is a footer added with Go"
    insertTextReq := &docs.Request{
        InsertText: &docs.InsertTextRequest{
            Text: footerText,
            EndOfSegmentLocation: &docs.EndOfSegmentLocation{
                SegmentId: "",
            },
        },
    }

But in the editor (I use vscode) there is no error but when I try to run, this error comes.

Error 400: Invalid value at 'requests[0].create_footer.type' (type.googleapis.com/google.apps.docs.v1.HeaderFooterType), "DEFAULT_FOOTER"Details:[{"@type": "type.googleapis.com/google.rpc.BadRequest","fieldViolations": [{"description": "Invalid value at 'requests[0].create_footer.type' (type.googleapis.com/google.apps.docs.v1.HeaderFooterType), "DEFAULT_FOOTER"","field": "requests[0].create_footer.type"}]}], invalidexit status 1

1 Answers1

1

You are giving incorrect value for the footer type, as per the library docs, (here) the allowed values are:

//   "HEADER_FOOTER_TYPE_UNSPECIFIED" - The header/footer type is
// unspecified.
//   "DEFAULT" - A default header/footer.

So, make the following changes, and it should work:

createFooterReq := &docs.Request{
    CreateFooter: &docs.CreateFooterRequest{
        SectionBreakLocation: &docs.Location{},
        Type:                 "DEFAULT",
    },
}
WhiteSword
  • 101
  • 9
  • I'm so grateful your answer, but İn the document I guess it didn't write in the footer wrote on the top left of the document and also when i run one more time this Error is come Error 400: Invalid requests[0].createFooter: Default footer already exists., badRequest exit status 1 – İkram MERT Apr 17 '23 at 10:27
  • @İkramMERT It seems like your doc already contains the footer. The doc says, `If a footer of the specified type already exists, a 400 bad request error is returned.` You can try testing with a new doc. – WhiteSword Apr 18 '23 at 06:17
  • @İkramMERT while inserting the text in a footer, you have to specify the footerID that you received after creating the footer, to the SegmentId variable. If you keep it empty, then the text will get inserted to the document body and not the footer. – WhiteSword Apr 18 '23 at 06:19
  • Try to get an idea from this post: https://stackoverflow.com/questions/57113349/how-to-add-header-footer-to-with-google-docs-api – WhiteSword Apr 18 '23 at 06:20