GSpread How To Duplicate Sheet
After googling and searching on Stackoveflow, I think I can't find a guide on how to duplicate existing sheet(existing Template sheet) and saving it into another sheet. as per docs
Solution 1:
- You want to copy the source Spreadsheet as new Spreadsheet.
- You want to achieve this using gspread with python.
- You have already been able to get and put values for Google Spreadsheet using Sheets API.
If my understanding is correct, how about this answer?
Issue and solution:
It seems that duplicate_sheet
method of gspread is used for copying a sheet in the source Spreadsheet to the same source Spreadsheet. Ref In order to copy the source Spreadsheet as new Spreadsheet, pleas use the method of copy()
of Class Client.
Sample script:
Please modify your script as follows.
From:client = gspread.authorize(credentials)
spreadsheet_client = Spreadsheet(client)
spreadsheet_client.duplicate_sheet("18Qk5bzuA7JOBD8CTgwvKYRiMl_35it5AwcFG2Bi5npo", new_sheet_name="timcard2")
worksheet = client.open("timcard2")
worksheet.share("my_email@google.com", perm_type='user', role='writer')
To:
client = gspread.authorize(credentials)
client.copy("18Qk5bzuA7JOBD8CTgwvKYRiMl_35it5AwcFG2Bi5npo", title="timcard2", copy_permissions=True)
worksheet = client.open("timcard2")
worksheet.share("my_email@google.com", perm_type='user', role='writer')
- When you run the script, the Spreadsheet which has the spreadsheet ID of
18Qk5bzuA7JOBD8CTgwvKYRiMl_35it5AwcFG2Bi5npo
is copied as the spreadsheet name oftimcard2
. And, the permission information of the source Spreadsheet is also copied.
Note:
- In this case, when
copy_permissions=True
is used, the permission information is also copied. So although I'm not sure about your actual situation, it might not be required to useworksheet.share("my_email@google.com", perm_type='user', role='writer')
. Please be careful this.
References:
Added:
- You want to copy one of sheets in Google Spreadsheet.
I could understand like above. For this, the sample script is as follows.
Sample script:
client = gspread.authorize(credentials)
client.copy("18Qk5bzuA7JOBD8CTgwvKYRiMl_35it5AwcFG2Bi5npo", title="timcard2", copy_permissions=True)
ss = client.open("timcard2")
ss.share("my_email@google.com", perm_type='user', role='writer')
delete_sheets = ["Sheet2", "Sheet3", "Sheet4"] # Please set the sheet names you want to delete.
for s in delete_sheets:
ss.del_worksheet(ss.worksheet(s))
- In this sample, the sheets of "Sheet2", "Sheet3", "Sheet4" are deleted from the copied Spreadsheet.
Post a Comment for "GSpread How To Duplicate Sheet"