[在Google上搜索并搜索Stackoveflow之后,我想找不到有关如何复制现有工作表(现有模板工作表)并将其保存到另一工作表的指南。
根据文档,有duplicate_sheet,但我无法管理一个有效的示例,任何可以指导我的示例?
import gspread
from gspread.models import Cell, Spreadsheet
scope = [
"https://www.googleapis.com/auth/spreadsheets.readonly",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive",
]
json_key_absolute_path = "key.json"
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key_absolute_path, scope)
client = gspread.authorize(credentials)
spreadsheet_client = Spreadsheet(client)
spreadsheet_client.duplicate_sheet("18Qk5bzuA7JOBD8CTgwvKYRiMl_35it5AwcFG2Bi5npo", new_sheet_name="timcard2")
worksheet = client.open("timcard2")
worksheet.share("[email protected]", perm_type='user', role='writer')
如果我的理解正确,那么这个答案如何?请认为这只是几个可能的答案之一。
似乎gspread的duplicate_sheet
方法用于将源Spreadsheet中的工作表复制到同一源Spreadsheet。 Ref为了将源电子表格复制为新电子表格,请使用Class Client的copy()
方法。
copy()
client = gspread.authorize(credentials)
client.copy("18Qk5bzuA7JOBD8CTgwvKYRiMl_35it5AwcFG2Bi5npo", title="timcard2", copy_permissions=True)
worksheet = client.open("timcard2")
worksheet.share("[email protected]", perm_type='user', role='writer')
时,权限信息也会被复制。因此,尽管我不确定您的实际情况,但可能不需要使用copy_permissions=True
。请注意这一点。worksheet.share("[email protected]", perm_type='user', role='writer')
如果我误解了你的问题,而这不是你想要的方向,我深表歉意。