Google Sheet API v4,将格式从一个电子表格复制并粘贴到另一个电子表格

时间:2017-03-11 16:52:18

标签: python google-sheets-api

此处的示例:https://developers.google.com/sheets/api/samples/data仅允许您复制和粘贴SAME电子表格的格式化操作。

我正在尝试在多个电子表格中复制粘贴格式。有办法吗?

3 个答案:

答案 0 :(得分:1)

目前尚不支持此功能。即使来自电子表格应用脚本的Class Range也不提供此类功能。尝试提交request here

答案 1 :(得分:0)

我找到了解决方法。这是:

我想说我想在" test"中复制范围的格式。从电子表格A到电子表格B的工作表具有相同的工作表名称。

  1. 复制表格" test"从电子表格A到B service.spreadsheets().sheets().copyTo(spreadsheetId=source,sheetId=id, body=data).execute()

  2. 既然工作表在同一个电子表格中,请使用https://developers.google.com/sheets/api/samples/data复制格式。

  3. 删除工作表"复制测试" spreadsheets().batchUpdate(spreadsheetId=key, body=data).execute()

答案 2 :(得分:0)

我通过使用spreadsheet.get来解决这个问题,以便使所有单元格成型。比我在每个单元格中构建了一个要在batchUbdate功能上使用的对象主体。

以下代码是使用python lib的示例:

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()