簡體   English   中英

使用 Python 使用 Google API 將電子表格工作表復制到另一個工作表

[英]Copy spreadsheet sheet to a another one with Google API using Python

我正在嘗試從模板創建一個谷歌電子表格,然后編輯它的值(單元格)。 手動,我只是訪問原始電子表格,然后從“文件”菜單中單擊“制作副本”。 我還沒有找到使用 Python3 和 gspread 做到這一點的方法。 所以,我試圖找到一種解決方法。

因此,我正在使用 Python 腳本來創建新的谷歌表,如代碼段所示(我在這里僅將其用作此問題的查看器):

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

它仍然無法正常工作。

請注意,出於測試目的,我將文件的權限更改為全局: 在此處輸入圖片說明

下面的修改怎么樣?

我認為該錯誤意味着"destinationSpreadsheetId": "1d8CeUxukBIOUpADE6eBlaksS2ptBjI0vIRpVm8ufEbs"是錯誤的。 當它看到電子表格.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 ,則腳本可以正常工作。

從上面的結果,您能否再次確認您要復制的電子表格的權限?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM