我有两个文件在谷歌驱动器(两个谷歌表),我需要从一个文件到另一个文件的数据和格式在一个常规模式
如下图所示,我需要维护不同的文本格式(粗体、文本颜色等):
我的第一次尝试是: 使用IMPORTRANGE仅使用google驱动表函数。它可以很好地复制数据,但我不想在目标文件中保留格式
我的第二个尝试是: 使用Python和gspread包将数据和格式从源googlesheet复制到目标googlesheet。我有以下代码:
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
我怎样才能解决我的问题
谢谢你的帮助
我相信你的目标和现状如下
修改点:
不幸的是,batchUpdate方法的“CopyPasteRequest”无法从Google电子表格复制到其他Google电子表格。这似乎是当前的规范
为了将值以及单元格格式从Google电子表格“A”复制到Google电子表格“B”,我想提出以下流程
当上述各点反映到脚本中时,它将变成如下所示
示例脚本:
在这个示例脚本中,我准备了下面的脚本
client = gspread.authorize(credentials)
,如下所示。在使用之前,请设置变量参考文献:
相关问题 更多 >
编程相关推荐