使用其 API 和 API 在 Google Sheets 电子表格中设置单元格格式Python

发布于 2024-09-01 01:17:17 字数 145 浏览 3 评论 0原文

我正在使用 gd_client.UpdateCell 更新谷歌电子表格中的值,并正在寻找一种方法来更新单元格的格式,即颜色、线条、文本大小......

这可以完成吗?

有可用的示例或其他文档吗?

任何能让我继续前进的事情都会很棒

I am using gd_client.UpdateCell to update values in a google spreadsheet and am looking for a way to update the formatting of the cell i.e. color, lines, textsize.....

Can this be done?

Are there examples or other documentation available?

Anything to get me going would be great

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别在捏我脸啦 2024-09-08 01:17:17

(2017 年 2 月) 作为在 Google I/O 2016 大会上,开发者现在可以使用最新的 API (v4) 设置 Google 表格中的单元格格式。下面是一个简短的 Python 示例,该示例将第一行加粗(假设文件 ID 为 SHEET_ID 并且 SHEETS 是 API 服务端点):

DATA = {'requests': [
    {'repeatCell': {
        'range': {'endRowIndex': 1},
        'cell':  {'userEnteredFormat': {'textFormat': {'bold': True}}},
        'fields': 'userEnteredFormat.textFormat.bold',
    }}
]}

SHEETS.spreadsheets().batchUpdate(
        spreadsheetId=SHEET_ID, body=DATA).execute()

我还制作了一个关于此主题的开发人员视频,如果这有帮助(见下文)。顺便说一句,您不仅可以使用 Python,还可以使用 Google API 客户端库。

最新的 Sheets API 提供了旧版本中不提供的功能,即让开发人员能够以编程方式访问工作表,就像您正在使用用户界面(冻结行、单元格格式[!]、调整行/列大小、添加数据透视表、创建图表等)。如果您是 API 新手,我创建了一些视频,其中包含更多“真实世界”示例:

如您所知,Sheets API 主要用于如上所述的面向文档的功能, 执行文件级访问,例如导入/导出、复制、移动、重命名等,请使用 Google Drive API 改为。

(Feb 2017) As of Google I/O 2016, developers can now format cells in Google Sheets using the latest API (v4). Here's a short Python example that bolds the 1st row (assuming the file ID is SHEET_ID and SHEETS is the API service endpoint):

DATA = {'requests': [
    {'repeatCell': {
        'range': {'endRowIndex': 1},
        'cell':  {'userEnteredFormat': {'textFormat': {'bold': True}}},
        'fields': 'userEnteredFormat.textFormat.bold',
    }}
]}

SHEETS.spreadsheets().batchUpdate(
        spreadsheetId=SHEET_ID, body=DATA).execute()

I also made a developer video on this subject if that helps (see below). BTW, you're not limited to Python, you can use any language supported by the Google APIs Client Libraries.

The latest Sheets API provides features not available in older releases, namely giving developers programmatic access to a Sheet as if you were using the user interface (frozen rows, cell formatting[!], resizing rows/columns, adding pivot tables, creating charts, etc.). If you're new to the API, I've created a few videos with somewhat more "real-world" examples:

As you can tell, the Sheets API is primarily for document-oriented functionality as described above, but to perform file-level access such as import/export, copy, move, rename, etc., use the Google Drive API instead.

赠我空喜 2024-09-08 01:17:17

我认为目前还不能做到这一点。当前的 API(List 和 Cell API)允许更改数据,但不允许格式化。

此处描述了整个 API。与格式无关:

许多人在群组中提出此请求,但从未得到 Google 的答复:

I don't think this can be done currently. The current APIs (both the List and Cell API) allow changing data, but not formatting.

The entire APIs are described here. Nothing about formatting:

Many people requesting this in the groups but never get an answer from Google:

假装爱人 2024-09-08 01:17:17

Google Apps 脚本看起来可能会带来一些这样的功能,特别是在 Range 类中:

https://developers.google.com/apps-script/reference/spreadsheet/range

重要的是,我还没有弄清楚如何将 Google Apps 脚本绑定(和/或执行)到我正在使用的工作表目前使用 Google Drive API 和 Google Sheets API 创建和填充。

我并不是建议将您的应用程序移植到 Google Apps 脚本,但我现在正在认真考虑这一点。我希望其他人对将 API 与 Google Apps 脚本连接起来的最后一个缺失部分有一些想法。

Google Apps Script looks like it might bring some of this functionality, in the Range class specifically:

https://developers.google.com/apps-script/reference/spreadsheet/range

Importantly, I have not figured out how to bind (and/or execute) a Google Apps Script to the sheet that I'm currently creating and filling using the Google Drive API and Google Sheets API.

I'm not suggesting porting your app to Google Apps Script, but I'm seriously considering it myself at this point. I'm hoping maybe someone else has some thoughts on the last missing piece of hooking the API up with the Google Apps Script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文