簡體   English   中英

使用 gspread 復制/粘貼格式

[英]Copy / Paste format using gspread

我正在嘗試使用gspread將格式從一個列復制/粘貼到另一個列。 我的工作表看起來像這樣:

在此處輸入圖像描述

我的結果應該是這樣的:

在此處輸入圖像描述

我試過了:

但出於某種原因,這不會復制格式,我不確定我的錯誤在哪里。

GSHEETS_SCOPES = [
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/drive.file",
    "https://www.googleapis.com/auth/drive"
]



CLIENT_SECRET_GOOGLE_SHEETS = r"folder/file.json"
creds = ServiceAccountCredentials.from_json_keyfile_name(CLIENT_SECRET_GOOGLE_SHEETS, GSHEETS_SCOPES)
client = gspread.authorize(creds)
sheet = client.open("My Sheet")

body = {
    "requests": [
        {
            "copyPaste": {
                "source": {
                    "sheetId": sheet.id,
                    "startRowIndex": 1,
                    "endRowIndex": 30,
                    "startColumnIndex": 0,
                    "endColumnIndex": 1
                },
                "destination": {
                    "sheetId": sheet,
                    "startRowIndex": 1,
                    "endRowIndex": 30,
                    "startColumnIndex": 1,
                    "endColumnIndex": 2
                },
                "pasteType": "PASTE_FORMAT"
            }
        },
        
        }
    ]
}
res = sheet.batch_update(body)

您的問題位於請求的正文中。

您應該提供要復制格式的目標工作表的工作表 ID,但您提供的是Worksheet python object。

這里:

...
"destination": {
  "sheetId": sheet,
...

或者它應該是:

...
"destination": {
  "sheetId": sheet.id,
...

暫無
暫無

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

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