[英]Python - GSPREAD - Copy text and format from one google sheet to another one
I have two files in google drive (two google sheets) and I need to move from one file to another the data and the format in a regular mode.我在谷歌驱动器中有两个文件(两个谷歌表),我需要以常规模式将数据和格式从一个文件移动到另一个文件。
As you can see on the picture bellow, I have different text formats that I need to maintain (bold, text color, etc.):正如您在下图中看到的,我需要维护不同的文本格式(粗体、文本颜色等):
My first attempt was: Using only google drive sheet functions using IMPORTRANGE.我的第一次尝试是:使用 IMPORTRANGE 仅使用谷歌驱动表功能。 It copies the data very well but I loose the format that I want to mantain on the destination file.它很好地复制了数据,但我丢失了我想在目标文件中保留的格式。
My second attemp has been: Using Python and gspread package copy the data and the format from source google sheet to the destination one.我的第二次尝试是:使用 Python 和 gspread package 将数据和格式从源谷歌表复制到目标表。 Fo that I have the following code:我有以下代码:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
source_file_sheet = 'https://docs.google.com/spreadsheets/X'
destination_file_sheet = 'https://docs.google.com/spreadsheets/Y'
service_key = "file.json"
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_file = ServiceAccountCredentials.from_json_keyfile_name(service_key, scope)
sourceSheetName = cod_source_system_file_operational
destinationSheetName = cod_source_system_file_billing
client = gspread.authorize(creds_file)
spreadsheet_source = client.open_by_url(source_file_sheet)
spreadsheet_destination = client.open_by_url(destination_file_sheet)
sourceSheetId = spreadsheet_source.worksheet('Sheet1')
destinationSheetId = spreadsheet_destination.worksheet('Sheet2')
body = {
"requests": [
{
"copyPaste": {
"source": {
"sheetId": sourceSheetId,
"startRowIndex": 3,
"endRowIndex": 10,
"startColumnIndex": 0,
"endColumnIndex": 5
},
"destination": {
"sheetId": destinationSheetId,
"startRowIndex": 0,
"endRowIndex": 10,
"startColumnIndex": 0,
"endColumnIndex": 5
},
"pasteType": "PASTE_NORMAL"
}
}
]
}
res = destinationSheetId.batch_update(body)
print(res)
But when I run this it gives me the following error:但是当我运行它时,它给了我以下错误:
Traceback (most recent call last):回溯(最近一次通话最后):
dict(vr, range=absolute_range_name(self.title, vr['range']))
TypeError: string indices must be integers
How can I solve my problem?我该如何解决我的问题?
Thanks for your help!谢谢你的帮助!
I believe your goal and your current situation as follows.我相信你的目标和你目前的情况如下。
Unfortunately, "CopyPasteRequest" of the batchUpdate method cannot copy from Google Spreadsheet to other Google Spreadsheet.遗憾的是,batchUpdate 方法的“CopyPasteRequest”无法从 Google 电子表格复制到其他 Google 电子表格。 It seems that this is the current specification.这似乎是当前的规范。
In order to copy not only the values, but also the cell format from Google Spreadsheet "A" to google Spreadsheet "B", I would like to propose the following flow.为了不仅将值,而且将单元格格式从谷歌电子表格“A”复制到谷歌电子表格“B”,我想提出以下流程。
When above points are reflected to a script, it becomes as follows.当以上几点反映到脚本时,它变成如下。
In this sample script, I prepared the script below client = gspread.authorize(credentials)
as follows.在这个示例脚本中,我在client = gspread.authorize(credentials)
下面准备了如下脚本。 Before you use this, please set the variables.在使用它之前,请设置变量。
client = gspread.authorize(credentials)
sourceSpreadsheetId = "###" # Please set the source Spreadsheet ID.
sourceSheetName = "Sheet1" # Please set the source sheet name.
destinationSpreadsheetId = "###" # Please set the destination Spreadsheet ID.
destinationSheetName = "Sheet2" # Please set the destination sheet name.
srcSpreadsheet = client.open_by_key(sourceSpreadsheetId)
srcSheet = srcSpreadsheet.worksheet(sourceSheetName)
dstSpreadsheet = client.open_by_key(destinationSpreadsheetId)
dstSheet = dstSpreadsheet.worksheet(destinationSheetName)
# 1. Copy the source sheet in the source Spreadsheet to the destination Spreadsheet.
copiedSheet = srcSheet.copy_to(destinationSpreadsheetId)
copiedSheetId = copiedSheet["sheetId"]
# 2. Copy the values with the format from the copied sheet to the destination sheet. And, delete the copied sheet.
body = {
"requests": [
{
"copyPaste": {
"source": {
"sheetId": copiedSheetId,
"startRowIndex": 3,
"endRowIndex": 10,
"startColumnIndex": 0,
"endColumnIndex": 5
},
"destination": {
"sheetId": dstSheet.id,
"startRowIndex": 0,
"endRowIndex": 10,
"startColumnIndex": 0,
"endColumnIndex": 5
},
"pasteType": "PASTE_NORMAL"
}
},
{
"deleteSheet": {
"sheetId": copiedSheetId
}
}
]
}
res = dstSpreadsheet.batch_update(body)
print(res)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.