python - 使用pygsheets使用service_file授权方法创建一个全新的sheet

标签 python google-sheets pygsheets

我已经可以使用 open_by_key() 函数编辑现有工作表,但我希望能够创建一个全新的工作表。

当我尝试以下代码时:

import pygsheets

creds_file = "/Users/jerome/audible2googlesheet.json"

gc = pygsheets.authorize(service_file=creds_file)
sh = gc.open("my_1st_public_sheet")
wks = sh.sheet1

# Update a single cell.                                                                                                                                                                                                                                                                                                               
wks.update_value('A1', "some value")

sh.share('', role='reader', type='anyone')

我收到此异常/错误:

raise SpreadsheetNotFound('Could not find a spreadsheet with title %s.' % title) pygsheets.exceptions.SpreadsheetNotFound: Could not find a spreadsheet with title my_1st_public_sheet.

我错过了什么?

最佳答案

  • 您想要使用 pygsheets 和 python 创建新工作表。

如果我的理解是正确的,这个答案怎么样?从你的问题,我可以理解为以下两种模式。

模式 1:

  • 您想要在现有电子表格中添加新工作表。

在本例中,示例脚本如下。

示例脚本:

import pygsheets

creds_file = "/Users/jerome/audible2googlesheet.json"

spreadsheet_key = '###'  # Please set the Spreadsheet ID.
gc = pygsheets.authorize(service_file=creds_file)
sh = gc.open("my_1st_public_sheet")
sh.add_worksheet("sampleTitle")  # Please set the new sheet name.

模式 2:

  • 您想要创建新的电子表格。

在本例中,示例脚本如下。

示例脚本:

import pygsheets

creds_file = "/Users/jerome/audible2googlesheet.json"

gc = pygsheets.authorize(service_file=creds_file)
res = gc.sheet.create("sampleTitle")  # Please set the new Spreadsheet name.
print(res)
  • 在这种情况下,新的电子表格将创建到服务驱动器帐户中。因此,如果您想在 Google 云端硬盘中查看创建的电子表格,请与您的 Google 帐户共享电子表格。请小心这一点。

注意:

  • 这些示例脚本假设您已经能够使用 Sheets API 获取和输入电子表格的值。

引用文献:

如果我误解了你的问题,我深表歉意。

添加:

在以下示例脚本中,将创建新的电子表格,并将创建的电子表格与您的帐户共享。

示例脚本:

在运行脚本之前,请将您的 Google 帐户电子邮件地址设置为 ### 您的电子邮件地址 ###

import pygsheets

creds_file = "/Users/jerome/audible2googlesheet.json"

gc = pygsheets.authorize(service_file=creds_file)
res = gc.sheet.create("sampleTitle")  # Please set the new Spreadsheet name.
createdSpreadsheet = gc.open_by_key(res['spreadsheetId'])
createdSpreadsheet.share('### your email address ###', role='writer', type='user')

关于python - 使用pygsheets使用service_file授权方法创建一个全新的sheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59110860/

相关文章:

Python-从URL、里面的控制和特殊字符获取JSON

javascript - sendemail的Google AppScript语法错误。无法识别我的问题

python - pygsheets 卡住第一行

python - 正则表达式到 FindAll 字符串链,直到带有 python 的点

python - 带极轴的条形图

google-apps-script - 在谷歌脚本中使用自定义顺序对列进行排序

python - pygsheets - 从字典而不是文件加载凭据

python - 如何使用 pygsheets 旋转 Google 表格中的文本?

python - 在 django 站点中嵌入 matplotlib 图

google-sheets - QUERY Google Sheets 函数中同一字符串中的引号和撇号