首页 > 解决方案 > 如何使用 gspread 或其他方式将值从一个电子表格复制到另一个?

问题描述

(初学者)我正在尝试使用 python 将值从一个电子表格复制到另一个电子表格。我正在使用 gspread 但我似乎无法弄清楚如何将值从我的第一个电子表格复制到另一个电子表格。如何从第一个电子表格中复制值并使用 python 将其粘贴到另一个电子表格中?

这是更新的代码:import gspread

from oauth2client.service_account import ServiceAccountCredentials


scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('sheetproject-956vg2854670.json',scope)
client = gspread.authorize(creds)






spreadsheetId= "1CkeZ8Xw4Xmho-mrFP9teHWVT-unkApzMSUFql5KkrGI"
sourceSheetName = "python test"
destinationSheetName = "python csv"

client = gspread.authorize(creds)
spreadsheet = client.open_by_key(spreadsheetId)
sourceSheetId = spreadsheet.worksheet("python test")._properties['0']. #gid value in the url
destinationSheetId = spreadsheet.worksheet("python csv")._properties['575313690'] #gid value in the url
body = {
    "requests":  [
        {
            "copypaste": {
                "source": {
                    "sheetId": 0,

                    "startRowIndex": 0,
                    "endRowIndex": 20,
                    "startColumnIndex": 0,
                    "endcolumnIndex": 1
                },
                "destination": {
                    "sheetId": 575313690,
                    "startRowIndex": 0,
                    "endRowIndex": 20,
                    "startColumnIndex": 0,
                    "endcolumnIndex": 1
                },
                "pasteType": "Paste_Normal"
            }
        }
    ]
}
res = spreadsheet.batch_update(body)
print(res)

标签: pythongoogle-sheetsgoogle-sheets-apigspread

解决方案


  • 您想要将值从工作表复制到 Google 电子表格中的其他工作表。
  • 您想使用 gspread 和 python 来实现这一点。
  • 您已经能够使用 Google Sheets API 获取和放置 Google 电子表格的值。

如果我的理解是正确的,这个答案怎么样?请认为这只是几个可能的答案之一。

在这个答案中,我想建议batch_update用于将值从工作表复制到电子表格中的其他工作表。在这种情况下,您的目标可以通过一个 API 调用来实现。

示例脚本:

在这个示例脚本中,授权脚本被删除。显示了用于将值从工作表复制到电子表格中的其他工作表的示例脚本。所以当你使用这个脚本时,请添加授权脚本,然后运行脚本。

spreadsheetId = "###"  # Please set the Spreadsheet ID.
sourceSheetName = "Sheet1"  # Please set the sheet name of source sheet.
destinationSheetName = "Sheet2"  # Please set the sheet name of destination sheet.

client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(spreadsheetId)
sourceSheetId = spreadsheet.worksheet(sourceSheetName)._properties['sheetId']
destinationSheetId = spreadsheet.worksheet(destinationSheetName)._properties['sheetId']
body = {
    "requests": [
        {
            "copyPaste": {
                "source": {
                    "sheetId": sourceSheetId,
                    "startRowIndex": 0,
                    "endRowIndex": 5,
                    "startColumnIndex": 0,
                    "endColumnIndex": 5
                },
                "destination": {
                    "sheetId": destinationSheetId,
                    "startRowIndex": 0,
                    "endRowIndex": 5,
                    "startColumnIndex": 0,
                    "endColumnIndex": 5
                },
                "pasteType": "PASTE_VALUES"
            }
        }
    ]
}
res = spreadsheet.batch_update(body)
print(res)

笔记:

  • 在此示例脚本中,A1:E5“Sheet1”工作表中的单元格的值被复制到A1:E5“Sheet2”工作表中的单元格中。
  • 在这种情况下,范围由GridRange给出。
  • 这是一个简单的示例脚本。所以请根据您的实际情况进行修改。

参考:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。


推荐阅读