代码之家  ›  专栏  ›  技术社区  ›  Datacrawler

使用Python使用Google API将电子表格复制到另一个电子表格

  •  4
  • Datacrawler  · 技术社区  · 8 年前

    我试图从一个模板创建一个谷歌电子表格,然后编辑它的值(单元格)。手动操作,我只需访问原始电子表格并单击 复制一份 文件 菜单我还没有找到使用Python3和gspread实现这一点的方法。因此,我正试图找到一个解决方法。

    因此,我使用Python脚本来创建新的google表单,如代码段所示(我在这里仅将其用作这个问题的查看器):

    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'], 'xxx@xxx-182311.iam.gserviceaccount.com', perm_type='user', role='owner')
    gc.insert_permission(response['spreadsheetId'], 'xxx.xxx@gmail.com', 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)
    

    它仍然不起作用。

    请注意,出于测试目的,我将文件的权限更改为全局: enter image description here

    1 回复  |  直到 8 年前
        1
  •  2
  •   Tanaike    8 年前

    我认为这个错误意味着 "destinationSpreadsheetId": "1d8CeUxukBIOUpADE6eBlaksS2ptBjI0vIRpVm8ufEbs" 是错误的。当它看到 the document of spreadsheets.sheets.copyTo {"destinationSpreadsheetId": ""} . 因此,请尝试以下修改。

    copy_sheet_to_another_spreadsheet_request_body = {
        {
      "destinationSpreadsheetId": "1d8CeUxukBIOUpADE6eBlaksS2ptBjI0vIRpVm8ufEbs"
    }
    }
    

    收件人:

    copy_sheet_to_another_spreadsheet_request_body = {
      "destinationSpreadsheetId": "1d8CeUxukBIOUpADE6eBlaksS2ptBjI0vIRpVm8ufEbs"
    }
    

    如果我误解了你的问题,对不起。

    编辑:

    1. 当然,当使用此脚本的用户是您要复制的电子表格的所有者时,脚本工作正常。
    2. 不是 要复制的电子表格的所有者,
      • 如果电子表格未共享,则错误 "The caller does not have permission" 发生。此错误可能与您的情况相同。
      • 如果电子表格共享为 On - Anyone with the link

    根据以上结果,您能否确认要再次复制的电子表格的权限?