如何使用gspread或其他方式将值从一个电子表格复制到另一个电子表格? [英] How do you copy values from one spreadsheet to another using gspread or some other way?

查看:147
本文介绍了如何使用gspread或其他方式将值从一个电子表格复制到另一个电子表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

(beginner) I'm attempting to copy values from one spreadsheet to another using python. I'm using gspread but I can't seem to figure out how to copy the values from my first spreadsheet to the other. How can I copy values from the first spreadsheet and paste it on the other using python?

这是更新的代码:import gspread

Here is the updated code: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)

推荐答案

  • 您想要将值从工作表复制到Google Spreadsheet中的其他工作表.
  • 您想通过gspread和python实现此目的.
  • 您已经能够使用Google Sheets API获取和放置Google Spreadsheet的值.
  • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.

    If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

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

    In this answer, I would like to propose to use batch_update for copying the values from from a sheet to other sheet in the Spreadsheet. In this case, your goal can be achieved by one API call.

    在此示例脚本中,删除了授权脚本.显示了用于将值从工作表复制到电子表格中其他工作表的示例脚本.因此,当您使用此脚本时,请添加授权脚本,然后运行该脚本.

    In this sample script, the script of authorization is removed. The sample script for copying values from a sheet to other sheet in the Spreadsheet is shown. So when you use this script, please add the authorization script, and run the script.

    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)
    

    注意:

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

      • In this sample script, the values of cells of A1:E5 in the sheet of "Sheet1" are copied to the cells of A1:E5 in the sheet of "Sheet2".
      • In this case, the range is given by the GridRange.
      • This is a simple sample script. So please modify this for your actual situation.
        • batch_update
        • Method: spreadsheets.batchUpdate
        • CopyPasteRequest
        • GridRange

        如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

        If I misunderstood your question and this was not the direction you want, I apologize.

        这篇关于如何使用gspread或其他方式将值从一个电子表格复制到另一个电子表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆