如何在Google Docs API上添加页脚

huangapple go评论78阅读模式
英文:

How to add a Footer on Google Docs API

问题

我正在尝试在Go中为我的文档添加页脚。以下是我的代码。

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

// 将文本插入页脚
footerText := "This is a footer added with Go"
insertTextReq := &docs.Request{
	InsertText: &docs.InsertTextRequest{
		Text: footerText,
		EndOfSegmentLocation: &docs.EndOfSegmentLocation{
			SegmentId: "",
		},
	},
}

但是在编辑器中(我使用的是vscode),没有错误,但是当我尝试运行时,出现了以下错误。

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
英文:

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

得分: 1

您为页脚类型提供了错误的值,根据库文档(这里),允许的值为:

// "HEADER_FOOTER_TYPE_UNSPECIFIED" - 未指定页眉/页脚类型。
// "DEFAULT" - 默认页眉/页脚。

因此,请进行以下更改,应该可以正常工作:

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

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",
    },
}

huangapple
  • 本文由 发表于 2023年4月17日 16:06:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定