"""台帳の全タブを、人が見て読みやすい形にそろえる（折り返し・縦の中央・罫線・数の寄せ・行の高さ）。
2026-11-23 に update_spreadsheet へ送った要求を、同じ中身で書き出す。
使い方: python3 wrap.py > wrap.json （中身を Sheets の update_spreadsheet の requests に渡す）
"""
import json

GRAY = {"red": 0.75, "green": 0.75, "blue": 0.75}
B = {"style": "SOLID", "colorStyle": {"rgbColor": GRAY}}


def gr(s, r0=None, r1=None, c0=None, c1=None):
    d = {"sheetId": s}
    if r0 is not None: d["startRowIndex"] = r0
    if r1 is not None: d["endRowIndex"] = r1
    if c0 is not None: d["startColumnIndex"] = c0
    if c1 is not None: d["endColumnIndex"] = c1
    return d


def wrap(rng, padding=False):
    fmt = {"wrapStrategy": "WRAP", "verticalAlignment": "MIDDLE"}
    fields = "userEnteredFormat.wrapStrategy,userEnteredFormat.verticalAlignment"
    if padding:
        fmt["padding"] = {"top": 4, "bottom": 4, "left": 6, "right": 6}
        fields += ",userEnteredFormat.padding"
    return {"repeatCell": {"range": rng, "cell": {"userEnteredFormat": fmt}, "fields": fields}}


def borders(rng):
    return {"updateBorders": {"range": rng, "top": B, "bottom": B, "left": B, "right": B,
                              "innerHorizontal": B, "innerVertical": B}}


def align(rng, a):
    return {"repeatCell": {"range": rng, "cell": {"userEnteredFormat": {"horizontalAlignment": a}},
                           "fields": "userEnteredFormat.horizontalAlignment"}}


def width(s, c0, c1, px):
    return {"updateDimensionProperties": {"range": {"sheetId": s, "dimension": "COLUMNS", "startIndex": c0, "endIndex": c1},
                                          "properties": {"pixelSize": px}, "fields": "pixelSize"}}


def height(s, r0, r1, px):
    return {"updateDimensionProperties": {"range": {"sheetId": s, "dimension": "ROWS", "startIndex": r0, "endIndex": r1},
                                          "properties": {"pixelSize": px}, "fields": "pixelSize"}}


def autorows(s, r0, r1):
    return {"autoResizeDimensions": {"dimensions": {"sheetId": s, "dimension": "ROWS", "startIndex": r0, "endIndex": r1}}}


R = [
    # 全タブ：本文を折り返し、縦の中央にそろえる（読み方は全体・ほかは見出しの下から）
    wrap(gr(0), padding=True),
    wrap(gr(1, 1)), wrap(gr(2, 1)), wrap(gr(3, 2)), wrap(gr(4, 1)),
    # 読み方：題の行だけは折り返さずに右へはみ出させる
    {"repeatCell": {"range": gr(0, 0, 1, 0, 2), "cell": {"userEnteredFormat": {"wrapStrategy": "OVERFLOW_CELL"}},
                    "fields": "userEnteredFormat.wrapStrategy"}},
    width(0, 0, 1, 180), width(0, 1, 2, 640), height(0, 0, 1, 40),
    borders(gr(0, 2, 7, 0, 2)), borders(gr(0, 8, 13, 0, 2)), borders(gr(0, 14, 18, 0, 2)),
    {"repeatCell": {"range": gr(0, 3, 18, 0, 1), "cell": {"userEnteredFormat": {"textFormat": {"bold": True}}},
                    "fields": "userEnteredFormat.textFormat.bold"}},
    # 週ごと
    borders(gr(1, 0, 5, 0, 19)), align(gr(1, 1, None, 1, 18), "RIGHT"),
    height(1, 0, 1, 64), width(1, 18, 19, 300),
    # 日ごと
    borders(gr(2, 0, 29, 0, 10)), align(gr(2, 1, None, 1, 9), "RIGHT"),
    width(2, 1, 9, 100), width(2, 9, 10, 360), height(2, 0, 1, 64),
    # ガイドSEO
    borders(gr(3, 0, 19, 0, 32)), align(gr(3, 2, None, 3, 5), "CENTER"), align(gr(3, 2, None, 6, 32), "CENTER"),
    width(3, 1, 2, 230), width(3, 2, 3, 240), width(3, 5, 6, 260), width(3, 6, 8, 100), height(3, 0, 1, 48),
    # キーワード
    borders(gr(4, 0, 39, 0, 7)), align(gr(4, 1, None, 1, 4), "CENTER"), align(gr(4, 1, None, 5, 6), "CENTER"),
    width(4, 4, 5, 260), width(4, 6, 7, 300),
    # 行の高さを中身に合わせる
    autorows(0, 1, 18), autorows(1, 1, 5), autorows(2, 1, 29), autorows(3, 2, 19), autorows(4, 1, 39),
]

print(json.dumps(R, ensure_ascii=False))
