GSpread如何复制工作表 [英] GSpread how to duplicate sheet

查看:99
本文介绍了GSpread如何复制工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google搜索和搜索Stackoveflow之后,我想找不到有关如何复制现有工作表(现有模板工作表)并将其保存到另一工作表的指南.

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.

根据文档,有 duplicate_sheet 但是我无法做一个可行的例子,有谁可以指导我吗?

as per docs, there is duplicate_sheet but I can't manage to do a working example, anyone that can guide me with this?

 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("my_email@google.com", perm_type='user', role='writer')

推荐答案

  • 您要将源电子表格复制为新的电子表格.
  • 您想通过gspread和python实现此目的.
  • 您已经能够使用Sheets API获取和放置Google Spreadsheet的值.
  • 如果我的理解正确,那么这个答案如何?

    If my understanding is correct, how about this answer?

    似乎gspread的 duplicate_sheet 方法用于将源电子表格中的工作表复制到同一源电子表格中.参考以便将源电子表格复制为新的电子表格请使用 copy() 类客户端.

    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.

    请按如下所示修改脚本.

    Please modify your script as follows.

    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')
    

    至:

    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')
    

    • 运行脚本时,将电子表格ID为 18Qk5bzuA7JOBD8CTgwvKYRiMl_35it5AwcFG2Bi5npo 的电子表格复制为 timcard2 的电子表格名称.并且,还将复制源电子表格的权限信息.
      • When you run the script, the Spreadsheet which has the spreadsheet ID of 18Qk5bzuA7JOBD8CTgwvKYRiMl_35it5AwcFG2Bi5npo is copied as the spreadsheet name of timcard2. And, the permission information of the source Spreadsheet is also copied.
        • 在这种情况下,当使用 copy_permissions = True 时,还将复制权限信息.因此,尽管我不确定您的实际情况,但可能不需要使用 worksheet.share("my_email@google.com",perm_type ='user',role ='writer').请注意这一点.
        • 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 use worksheet.share("my_email@google.com", perm_type='user', role='writer'). Please be careful this.
        • 您要复制Google Spreadsheet中的一张工作表.

        我可以像上面那样理解.为此,示例脚本如下.

        I could understand like above. For this, the sample script is as follows.

        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))
        

        • 在该样本中,"Sheet2","Sheet3","Sheet4"的表被替换.从复制的电子表格中删除.
        • 这篇关于GSpread如何复制工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆