简体   繁体   English

如何使用 Drive API 在 Google 电子表格中获取工作表列表(名称和“gid”)?

[英]How can I get a list of sheets (name and "gid") in a Google spreadsheet using the Drive API?

I am trying to use Google Drive API (Python) to download some tabs of a spreadsheet file.我正在尝试使用 Google Drive API (Python) 下载电子表格文件的一些选项卡。 Is the gids information in the file's metadata?文件元数据中是否包含 gids 信息? What I am trying to do is (this may not be correct, please suggest):我想做的是(这可能不正确,请建议):

file_metadata = self.service.files().get(file_id=file_id).execute()
# in the following line, how can I get a download url with a gid (a tab in a spreadsheet file)
download_url = file_metadata.get('exportLinks')['text/csv']
# download the file.

Indeed this seems to be a duplicate of How to convert Google spreadsheet's worksheet string id to integer index (GID)? 事实上,这似乎是如何将Google电子表格的工作表字符串ID转换为整数索引(GID)的副本

Here is the quick answer: 这是快速回答:

def to_gid(worksheet_id):
    return int(worksheet_id, 36) ^ 31578

You can do this using the Spreadsheet service in Google Apps Script. 您可以使用Google Apps脚本中的电子表格服务执行此操作。 It's not as convenient as having a direct API call, but at least it is possible: 它不像直接API调用那么方便,但至少可以:

  1. Call the SpreadsheetApp.openById(id) method to open the spreadsheet using the Drive file_id . 调用SpreadsheetApp.openById(id)方法以使用Drive file_id打开电子表格。
  2. Call the Spreadsheet.getSheets() method to retrieve a list of Sheet objects. 调用Spreadsheet.getSheets()方法以检索Sheet对象的列表。
  3. For each sheet, call the Sheet.getSheetId() and Sheet.getSheetName() methods to get the name (which is what the spreadsheet user sees) and the sheetId (which is what needs to be provided in the gid= parameter when exporting). 对于每个工作表,调用Sheet.getSheetId()Sheet.getSheetName()方法来获取名称(电子表格用户看到的名称)和sheetId(导出时需要在gid=参数中提供的名称) 。

You can use the recently-announced Apps Script Execution API to provide a REST API for your Apps Script project, which you can then call in a similar manner to the Drive API. 您可以使用最近公布的Apps Script Execution API为您的Apps脚本项目提供REST API,然后您可以通过与Drive API类似的方式调用该API。

You can use an old visualization API URL您可以使用旧的可视化 API URL

f'https://docs.google.com/spreadsheets/d/{doc_id}/gviz/tq?tqx=out:csv&sheet={sheet_name}'

to download a sheet by its name.按名称下载工作表。 I just posted this here together with code for the Google API Python library.我刚刚在此处发布了此内容以及 Google API Python 库的代码。 This is also mentioned in another answer which links to the Charts docs where it is shown how to do this with a gid . 另一个答案中也提到了这一点,该答案链接到Charts 文档,其中显示了如何使用gid执行此操作。 Alternatively, you can download all sheets by exporting the spreadsheet as an eg Excel file,或者,您可以通过将电子表格导出为例如 Excel 文件来下载所有工作表,

class MimeTypes:
    EXCEL = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

drive_api.files().export_media(fileId=file_id, mimeType=MimeTypes.EXCEL)

If you still want to go with getting sheets by gid , the gid is the sheetId in the Google Sheets API (see here ).如果您仍然希望 go 通过gid获取工作表,则gid是 Google Sheets sheetId中的 sheetId(请参见 此处)。

And you should be able to use the Sheets API to get all the sheets - and their corresponding sheetId s.您应该能够使用表格 API来获取所有表格 - 以及它们对应的sheetId

I have a radical idea: given that you intend to automate things a bit and already know (at least some) python, why not export data from the tabs you need to CSV files, download them, and then import as pandas.DataFrame s. I have a radical idea: given that you intend to automate things a bit and already know (at least some) python, why not export data from the tabs you need to CSV files, download them, and then import as pandas.DataFrame s. After all, these data structures have been modeled after Google (and Excel) Sheets and are equally intuitive to use.毕竟,这些数据结构是根据 Google(和 Excel)表格建模的,并且使用起来同样直观。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用一个 API 调用从谷歌电子表格中的所有工作表(选项卡)中获取所有记录? - How do I get all records from all sheets (tabs) in a google spreadsheet using one API call? 如何使用 python 和谷歌表格 API 从谷歌表格 ID 中获取谷歌表格名称? - How can I fetch Google Sheet name from Google sheet ID using python and google sheets API? 如何使用 Google 表格 API 在电子表格中创建新表格 - How to create a new sheet within a spreadsheet using Google Sheets API 如何使用 Python 中的 API 重命名 Google Sheets 电子表格中的(工作)工作表? - How do I rename a (work)sheet in a Google Sheets spreadsheet using the API in Python? 如何更改Google表格电子表格的所有者? - How can I change the owner of a Google Sheets spreadsheet? 如何使用驱动器 API 和 Python 中的服务帐户将电子表格中的所有工作表导出为 CSV 文件? - How to export all sheets in a Spreadsheet as CSV files using the Drive API with a Service Account in Python? 如何使用用于 Google 表格的 ezsheets API 冻结一行? - How can I freeze a row using the ezsheets API for Google Sheets? 如何获取 token-sheets.pickle 和 token-drive.pickle 文件以将 ezsheets 用于 Google 表格? - How can I get the token-sheets.pickle and token-drive.pickle files to use ezsheets for Google Sheets? 在 Python 中获取 google 电子表格 api v4 中的工作表和最新工作表列表 - Get list of sheets and latest sheet in google spreadsheet api v4 in Python 无法使用python通过google drive API下载google电子表格 - Not able to download google spreadsheet by google drive API using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM