繁体   English   中英

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

[英]Google sheets API v4, copy and paste format from one spreadsheet to another

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

我正在尝试跨多个电子表格复制粘贴格式。 有没有办法做到这一点?

尚不支持此功能。 即使是电子表格应用程序脚本中的Class 范围也不提供此类功能。 尝试在这里提出请求

我设法通过使用电子表格来解决这个问题。get 让所有单元格格式化。 然后,我遍历每个单元构建一个 object 主体以用于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()

我找到了解决办法。 这里是:

假设我想将“测试”工作表中的范围格式从电子表格 A 复制到具有相同工作表名称的电子表格 B。

  1. 使用service.spreadsheets().sheets().copyTo(spreadsheetId=source,sheetId=id, body=data).execute()将工作表“测试”从电子表格 A 复制到 B

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

  3. 使用spreadsheets().batchUpdate(spreadsheetId=key, body=data).execute()删除工作表“测试副本”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM