Google sheets API v4,将格式从一个电子表格复制并粘贴到另一个电子表格
Google sheets API v4, copy and paste format from one spreadsheet to another
此处示例:https://developers.google.com/sheets/api/samples/data 仅允许您对同一电子表格进行复制和粘贴格式操作。
我正在尝试跨多个电子表格复制粘贴格式。有办法吗?
尚不支持此功能。甚至 Class Range from Spreadsheet Apps Script doesn't offer such feature. Try to file a request here.
我找到了解决方法。这是:
假设我想将 "test" sheet 中的一个范围的格式从 spreadsheet A 复制到 spreadsheet B 与相同的 sheet 名字.
使用service.spreadsheets().sheets().copyTo(spreadsheetId=source,sheetId=id, body=data).execute()
将sheet"test"从spreadsheetA复制到B
现在 sheet 处于相同的传播sheet,使用 https://developers.google.com/sheets/api/samples/data 复制格式。
删除 sheet "Copy of test" 和 spreadsheets().batchUpdate(spreadsheetId=key, body=data).execute()
我设法解决了这个问题,方法是使用 spreadsheet.get 使所有单元格都具有格式。然后我遍历每个单元构建一个对象主体以用于 batchUbdate
功能。
下面的代码是使用 python 库的示例:
from httplib2 import Http
from apiclient.discovery import build
# Credentials object is obtained through the google oauth flow lib.
def transfer_first_tab_format(credentials, sourcce_spreadsheet_id, dest_spreadsheet_id):
http_auth = credentials.authorize(Http())
sheets_service = build(
'sheets',
version='v4',
http=http_auth
)
# --------- get source spreadsheet ----------- #
source_spreadsheet_info = sheets_service.spreadsheets().get(
spreadsheetId=sourcce_spreadsheet_id, ranges=[]
).execute()
source_sheets = source_spreadsheet_info.get("sheets")
source_tab = source_sheets[0]
source_tab_title = source_tab.get("properties", {}).get("title")
# ---------------------------- #
# --------- get destination spreadsheet ----------- #
dest_spreadsheet_info = sheets_service.spreadsheets().get(
spreadsheetId=dest_spreadsheet_id, ranges=[]
).execute()
dest_sheets = dest_spreadsheet_info.get("sheets")
dest_tab = dest_sheets[0]
dest_tab_id = dest_tab.get("properties", {}).get("sheetId")
# ---------------------------- #
end_col = end_row = source_tab.get("properties", {}).get("gridProperties", {}).get("columnCount")
# you could find a function that converts a number to alphabet letter,
# so you could get the letter from position end_col
letter_col = 'L' # the last column letter or num_to_letter(end_col)
start_row = 1
end_row = source_tab.get("properties", {}).get("gridProperties", {}).get("rowCount")
source_format = sheets_service.spreadsheets().get(
spreadsheetId=sourcce_spreadsheet_id,
ranges="{0}!A{1}:{2}{3}".format(source_tab_title, start_row, letter_col, end_row),
includeGridData=True
).execute()
source_rows = source_format.get("sheets", [{}])[0].get("data", {})[0].get("rowData")
format_rules = []
# --- BUILDS THE FORMAT RULES FOR BATCHUPDATE --- #
for row_index, source_row in enumerate(source_rows):
source_cells = source_row.get("values")
for col_index, source_cell in enumerate(source_cells):
source_cell_format = source_cell.get("effectiveFormat")
format_rule = {
"repeatCell": {
"range": {
"sheetId": dest_tab_id,
"startRowIndex": row_index, # it's 0 indexed. should start at 0
"endRowIndex": row_index + 1,
"startColumnIndex": col_index,
"endColumnIndex": col_index + 1,
},
"fields": "userEnteredFormat(backgroundColor, horizontalAlignment, textFormat, borders, "
"hyperlinkDisplayType, padding, verticalAlignment, wrapStrategy)",
"cell": {
"userEnteredFormat": source_cell_format
}
}
}
# builds the rules for batchupdate
format_rules.append(format_rule)
# ---- PERFORM THE BATCH UPDATE ---- #
body = {
'requests': [format_rules]
}
sheets_service.spreadsheets().batchUpdate(
spreadsheetId=dest_spreadsheet_id,
body=body
).execute()
此处示例:https://developers.google.com/sheets/api/samples/data 仅允许您对同一电子表格进行复制和粘贴格式操作。
我正在尝试跨多个电子表格复制粘贴格式。有办法吗?
尚不支持此功能。甚至 Class Range from Spreadsheet Apps Script doesn't offer such feature. Try to file a request here.
我找到了解决方法。这是:
假设我想将 "test" sheet 中的一个范围的格式从 spreadsheet A 复制到 spreadsheet B 与相同的 sheet 名字.
使用
service.spreadsheets().sheets().copyTo(spreadsheetId=source,sheetId=id, body=data).execute()
将sheet"test"从spreadsheetA复制到B
现在 sheet 处于相同的传播sheet,使用 https://developers.google.com/sheets/api/samples/data 复制格式。
删除 sheet "Copy of test" 和
spreadsheets().batchUpdate(spreadsheetId=key, body=data).execute()
我设法解决了这个问题,方法是使用 spreadsheet.get 使所有单元格都具有格式。然后我遍历每个单元构建一个对象主体以用于 batchUbdate
功能。
下面的代码是使用 python 库的示例:
from httplib2 import Http
from apiclient.discovery import build
# Credentials object is obtained through the google oauth flow lib.
def transfer_first_tab_format(credentials, sourcce_spreadsheet_id, dest_spreadsheet_id):
http_auth = credentials.authorize(Http())
sheets_service = build(
'sheets',
version='v4',
http=http_auth
)
# --------- get source spreadsheet ----------- #
source_spreadsheet_info = sheets_service.spreadsheets().get(
spreadsheetId=sourcce_spreadsheet_id, ranges=[]
).execute()
source_sheets = source_spreadsheet_info.get("sheets")
source_tab = source_sheets[0]
source_tab_title = source_tab.get("properties", {}).get("title")
# ---------------------------- #
# --------- get destination spreadsheet ----------- #
dest_spreadsheet_info = sheets_service.spreadsheets().get(
spreadsheetId=dest_spreadsheet_id, ranges=[]
).execute()
dest_sheets = dest_spreadsheet_info.get("sheets")
dest_tab = dest_sheets[0]
dest_tab_id = dest_tab.get("properties", {}).get("sheetId")
# ---------------------------- #
end_col = end_row = source_tab.get("properties", {}).get("gridProperties", {}).get("columnCount")
# you could find a function that converts a number to alphabet letter,
# so you could get the letter from position end_col
letter_col = 'L' # the last column letter or num_to_letter(end_col)
start_row = 1
end_row = source_tab.get("properties", {}).get("gridProperties", {}).get("rowCount")
source_format = sheets_service.spreadsheets().get(
spreadsheetId=sourcce_spreadsheet_id,
ranges="{0}!A{1}:{2}{3}".format(source_tab_title, start_row, letter_col, end_row),
includeGridData=True
).execute()
source_rows = source_format.get("sheets", [{}])[0].get("data", {})[0].get("rowData")
format_rules = []
# --- BUILDS THE FORMAT RULES FOR BATCHUPDATE --- #
for row_index, source_row in enumerate(source_rows):
source_cells = source_row.get("values")
for col_index, source_cell in enumerate(source_cells):
source_cell_format = source_cell.get("effectiveFormat")
format_rule = {
"repeatCell": {
"range": {
"sheetId": dest_tab_id,
"startRowIndex": row_index, # it's 0 indexed. should start at 0
"endRowIndex": row_index + 1,
"startColumnIndex": col_index,
"endColumnIndex": col_index + 1,
},
"fields": "userEnteredFormat(backgroundColor, horizontalAlignment, textFormat, borders, "
"hyperlinkDisplayType, padding, verticalAlignment, wrapStrategy)",
"cell": {
"userEnteredFormat": source_cell_format
}
}
}
# builds the rules for batchupdate
format_rules.append(format_rule)
# ---- PERFORM THE BATCH UPDATE ---- #
body = {
'requests': [format_rules]
}
sheets_service.spreadsheets().batchUpdate(
spreadsheetId=dest_spreadsheet_id,
body=body
).execute()