使用 google sheet api 复制和粘贴值

Copy & paste values using google sheet api

我想将 "'data'!A2:D2" 复制到 "'data'!K2:N2",但出现以下错误。该脚本适用于批量更新,但无法通过复制和粘贴解决。

{"error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"requests\": Cannot find field.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"requests\": Cannot find field."
          }]}]}}

我正在使用的代码。

import json
import gspread
import requests
from oauth2client.service_account import ServiceAccountCredentials

scope = [
    "https://spreadsheets.google.com/feeds",
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/drive",
]

creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
client = gspread.authorize(creds)
sh = client.open("kite")

spreadsheet_id = sh.id
sheet_id = sh.worksheet("data").id

headers = {
    "Authorization": "Bearer " + creds.get_access_token().access_token,
    "Content-Type": "application/json",
}

reqs = [
    {
        "copyPaste": {
            "source": {
                "sheetId": sheet_id,
                "startRowIndex": 1,
                "endRowIndex": 2,
                "startColumnIndex": 0,
                "endColumnIndex": 4,
            },
            "destination": {
                "sheetId": sheet_id,
                "startRowIndex": 1,
                "endRowIndex": 2,
                "startColumnIndex": 10,
                "endColumnIndex": 14,
            },
            "pasteType": "PASTE_VALUES",
            "pasteOrientation": "NORMAL",
        }
    }
]

r = requests.post(
    f"https://sheets.googleapis.com/v4/spreadsheets/{spreadsheet_id}/values:batchUpdate",
    headers=headers,
    data=json.dumps({"requests": reqs}),
)

参考: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#CopyPasteRequest

copyPaste 请求可以与 spreadsheets.batchUpdate 方法一起使用。所以,当您的脚本修改时,请修改如下。

发件人:

f"https://sheets.googleapis.com/v4/spreadsheets/{spreadsheet_id}/values:batchUpdate",

收件人:

f"https://sheets.googleapis.com/v4/spreadsheets/{spreadsheet_id}:batchUpdate",

注:

  • 在您的脚本中,使用了 gspread。使用gspread时,你的脚本也可以修改如下。

      client = gspread.authorize(creds)
      sh = client.open("kite")
      sheet_id = sh.worksheet("data").id
      reqs = [
          {
              "copyPaste": {
                  "source": {
                      "sheetId": sheet_id,
                      "startRowIndex": 1,
                      "endRowIndex": 2,
                      "startColumnIndex": 0,
                      "endColumnIndex": 4,
                  },
                  "destination": {
                      "sheetId": sheet_id,
                      "startRowIndex": 1,
                      "endRowIndex": 2,
                      "startColumnIndex": 10,
                      "endColumnIndex": 14,
                  },
                  "pasteType": "PASTE_VALUES",
                  "pasteOrientation": "NORMAL",
              }
          }
      ]
      res = sh.batch_update({"requests": reqs})
    

参考: