我正在尝试从模板创建一个谷歌电子表格,然后编辑其值(单元格)。我只需手动访问原始电子表格并单击复印一份来自File菜单。我还没有找到使用 Python3 和 gspread 来做到这一点的方法。因此,我正在尝试寻找解决方法。
因此,我使用 Python 脚本来创建新的谷歌工作表,如代码片段所示(我仅将其用作此问题的查看器):
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint
from googleapiclient import discovery
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
gc = gspread.authorize(credentials)
service = discovery.build('sheets', 'v4', credentials=credentials)
spreadsheet_body = {
"properties": {
"title": "xxGoogleAPIMasterTemplatexx"
}
}
request = service.spreadsheets().create(body=spreadsheet_body)
response = request.execute()
#Changing permissions for the user (from the credentials.json) and then the real user (me)
gc.insert_permission(response['spreadsheetId'], '[email protected] /cdn-cgi/l/email-protection', perm_type='user', role='owner')
gc.insert_permission(response['spreadsheetId'], '[email protected] /cdn-cgi/l/email-protection', perm_type='user', role='owner')
新的电子表格称为xxGoogleAPIMasterTemplatexx正如我所料(脚本)。现在,我尝试将一个电子表格(原始模板)中的一张工作表复制到新创建的电子表格中。
我现在的步骤是首先测试是否copy_to_spreadsheet works
对于特定 ID。我使用了以下脚本:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint
from googleapiclient import discovery
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
gc = gspread.authorize(credentials)
# The ID of the spreadsheet containing the sheet to copy. Everybody has access!!!
spreadsheet_id = '1lw6FVG_gSDseDdseS54jOyXph74k_XfTvnyU2Wqd7yo'
#sheet = gc.open_by_key(response['spreadsheet_id']).Sheet1
# The ID of the sheet to copy. Everybody has access!!!
sheet_id = 0
copy_sheet_to_another_spreadsheet_request_body = {
{
"destinationSpreadsheetId": "1d8CeUxukBIOUpADE6eBlaksS2ptBjI0vIRpVm8ufEbs"
}
}
request = service.spreadsheets().sheets().copyTo(spreadsheetId=spreadsheet_id, sheetId=sheet_id, body=copy_sheet_to_another_spreadsheet_request_body)
response = request.execute()
我得到了这个错误:
"destinationSpreadsheetId": "1d8CeUxukBIOUpADE6eBlaksS2ptBjI0vIRpVm8ufEbs"
TypeError: unhashable type: 'dict'
我猜测 ID 必须是可哈希的。添加此行后:
key = frozenset(dict_key.spreadsheet_id)
它仍然不起作用。
Please note that for the test purposes I changed the permissions of the files to global:
![enter image description here](https://i.stack.imgur.com/yeuVl.png)
下面的修改怎么样?
我认为该错误意味着结构"destinationSpreadsheetId": "1d8CeUxukBIOUpADE6eBlaksS2ptBjI0vIRpVm8ufEbs"
是错的。当它看到右侧时Spreadsheets.sheets.copyTo 的文档 https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.sheets/copyTo,请求体是{"destinationSpreadsheetId": ""}
。所以请尝试以下修改。
From :
copy_sheet_to_another_spreadsheet_request_body = {
{
"destinationSpreadsheetId": "1d8CeUxukBIOUpADE6eBlaksS2ptBjI0vIRpVm8ufEbs"
}
}
To :
copy_sheet_to_another_spreadsheet_request_body = {
"destinationSpreadsheetId": "1d8CeUxukBIOUpADE6eBlaksS2ptBjI0vIRpVm8ufEbs"
}
如果我误解了你的问题,我很抱歉。
Edit :
对于下一个错误,我确认了以下模式。
- 当使用此脚本的用户是您要复制的电子表格的所有者时,当然,该脚本可以正常工作。
- When the user who uses this script is NOT the owner of the spreadsheet that you want to copy,
- 如果电子表格未共享,则会出现以下错误
"The caller does not have permission"
发生。此错误可能与您的情况相同。
- 如果电子表格共享为
On - Anyone with the link
,脚本运行良好。
从以上结果中,您能否确认您要再次复制的电子表格的权限?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)