GSpread如何复制工作表

问题描述 投票:0回答:1

[在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')
python gspread
1个回答
0
投票
  • 您要将源电子表格复制为新电子表格。
  • 您想通过gspread和python实现此目的。
  • 您已经能够使用Sheets API获取和放置Google Spreadsheet的值。

如果我的理解正确,那么这个答案如何?请认为这只是几个可能的答案之一。

问题和解决方法:

似乎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。请注意这一点。

参考:

  • duplicate_sheet
  • 如果我误解了你的问题,而这不是你想要的方向,我深表歉意。

    © www.soinside.com 2019 - 2024. All rights reserved.