Python - GSPREAD - 将文本和格式从一个 google sheet 复制到另一个
Python - GSPREAD - Copy text and format from one google sheet to another one
我在 google 驱动器中有两个文件(两个 google sheet),我需要以常规模式将数据和格式从一个文件移动到另一个文件。
如下图所示,我需要维护不同的文本格式(粗体、文本颜色等):
我的第一次尝试是:
使用 IMPORTRANGE 仅使用 google 驱动 sheet 函数。它很好地复制了数据,但我丢失了我想在目标文件上保留的格式。
我的第二次尝试是:
使用 Python 和 gspread 包将数据和格式从源 google sheet 复制到目标。我有以下代码:
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)
但是当我 运行 这给了我以下错误:
回溯(最近调用最后):
dict(vr, range=absolute_range_name(self.title, vr['range']))
TypeError: string indices must be integers
我该如何解决我的问题?
感谢您的帮助!
我认为您的目标和现状如下。
- 您想将 Google Spreadsheet 中特定 sheet 的值复制到 Google Spreadsheet 中的特定 sheet =70=] "B".
- 您不仅要复制值,还要复制单元格格式。
- 您想使用 python 的 gspread 实现此目的。
- 您已经能够使用 Sheets API.
获取和输入 Google Spreadsheet 的值
修改点:
不幸的是,batchUpdate 方法的“CopyPasteRequest”无法从 Google Spreadsheet 复制到其他 Google Spreadsheet。好像是现在的规格。
为了不仅复制值,还复制单元格格式,从 Google Spreadsheet "A" 到 google Spreadsheet "B",我想提出以下流程。
- 将源Spreadsheet中的源sheet复制到目标Spreadsheet.
- 将复制的 sheet 格式的值复制到目标 sheet。并且,删除复制的 sheet.
以上几点反映到一个脚本中,就变成了下面这样。
示例脚本:
在这个示例脚本中,我准备了client = gspread.authorize(credentials)
下面的脚本,如下所示。在使用之前,请设置变量。
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)
参考文献:
我在 google 驱动器中有两个文件(两个 google sheet),我需要以常规模式将数据和格式从一个文件移动到另一个文件。
如下图所示,我需要维护不同的文本格式(粗体、文本颜色等):
我的第一次尝试是: 使用 IMPORTRANGE 仅使用 google 驱动 sheet 函数。它很好地复制了数据,但我丢失了我想在目标文件上保留的格式。
我的第二次尝试是: 使用 Python 和 gspread 包将数据和格式从源 google sheet 复制到目标。我有以下代码:
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)
但是当我 运行 这给了我以下错误:
回溯(最近调用最后):
dict(vr, range=absolute_range_name(self.title, vr['range']))
TypeError: string indices must be integers
我该如何解决我的问题?
感谢您的帮助!
我认为您的目标和现状如下。
- 您想将 Google Spreadsheet 中特定 sheet 的值复制到 Google Spreadsheet 中的特定 sheet =70=] "B".
- 您不仅要复制值,还要复制单元格格式。
- 您想使用 python 的 gspread 实现此目的。
- 您已经能够使用 Sheets API. 获取和输入 Google Spreadsheet 的值
修改点:
不幸的是,batchUpdate 方法的“CopyPasteRequest”无法从 Google Spreadsheet 复制到其他 Google Spreadsheet。好像是现在的规格。
为了不仅复制值,还复制单元格格式,从 Google Spreadsheet "A" 到 google Spreadsheet "B",我想提出以下流程。
- 将源Spreadsheet中的源sheet复制到目标Spreadsheet.
- 将复制的 sheet 格式的值复制到目标 sheet。并且,删除复制的 sheet.
以上几点反映到一个脚本中,就变成了下面这样。
示例脚本:
在这个示例脚本中,我准备了client = gspread.authorize(credentials)
下面的脚本,如下所示。在使用之前,请设置变量。
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)