import json
def rgb(h): h=h.lstrip('#'); return {"red":int(h[0:2],16)/255,"green":int(h[2:4],16)/255,"blue":int(h[4:6],16)/255}
def gr(s,r0,r1,c0,c1):
    d={"sheetId":s,"startRowIndex":r0,"startColumnIndex":c0,"endColumnIndex":c1}
    if r1 is not None: d["endRowIndex"]=r1
    return d
B={"style":"SOLID","colorStyle":{"rgbColor":rgb("#BFBFBF")}}
R=[]
def hdr(s,r0,r1,c0,c1):
    R.append({"repeatCell":{"range":gr(s,r0,r1,c0,c1),"cell":{"userEnteredFormat":{"backgroundColorStyle":{"rgbColor":rgb("#333333")},"textFormat":{"bold":True,"foregroundColorStyle":{"rgbColor":rgb("#FFFFFF")}},"wrapStrategy":"WRAP","verticalAlignment":"MIDDLE","horizontalAlignment":"CENTER"}},"fields":"userEnteredFormat.backgroundColorStyle,userEnteredFormat.textFormat,userEnteredFormat.wrapStrategy,userEnteredFormat.verticalAlignment,userEnteredFormat.horizontalAlignment"}})
def body(s):
    R.append({"repeatCell":{"range":{"sheetId":s,"startRowIndex":1},"cell":{"userEnteredFormat":{"wrapStrategy":"WRAP","verticalAlignment":"MIDDLE"}},"fields":"userEnteredFormat.wrapStrategy,userEnteredFormat.verticalAlignment"}})
def nf(s,r0,r1,c0,c1,t,p): R.append({"repeatCell":{"range":gr(s,r0,r1,c0,c1),"cell":{"userEnteredFormat":{"numberFormat":{"type":t,"pattern":p}}},"fields":"userEnteredFormat.numberFormat"}})
def al(s,r0,r1,c0,c1,a): R.append({"repeatCell":{"range":gr(s,r0,r1,c0,c1),"cell":{"userEnteredFormat":{"horizontalAlignment":a}},"fields":"userEnteredFormat.horizontalAlignment"}})
def bd(s,r0,r1,c0,c1): R.append({"updateBorders":{"range":gr(s,r0,r1,c0,c1),"top":B,"bottom":B,"left":B,"right":B,"innerHorizontal":B,"innerVertical":B}})
def w(s,c0,c1,px): R.append({"updateDimensionProperties":{"range":{"sheetId":s,"dimension":"COLUMNS","startIndex":c0,"endIndex":c1},"properties":{"pixelSize":px},"fields":"pixelSize"}})
def h(s,r0,r1,px): R.append({"updateDimensionProperties":{"range":{"sheetId":s,"dimension":"ROWS","startIndex":r0,"endIndex":r1},"properties":{"pixelSize":px},"fields":"pixelSize"}})
def note(s,r,c1):
    R.append({"mergeCells":{"range":gr(s,r,r+1,0,c1),"mergeType":"MERGE_ALL"}})
    R.append({"repeatCell":{"range":gr(s,r,r+1,0,c1),"cell":{"userEnteredFormat":{"wrapStrategy":"WRAP","verticalAlignment":"TOP","textFormat":{"foregroundColorStyle":{"rgbColor":rgb("#555555")},"fontSize":9}}},"fields":"userEnteredFormat.wrapStrategy,userEnteredFormat.verticalAlignment,userEnteredFormat.textFormat"}})
# month tab 5
body(5); hdr(5,0,1,0,5); hdr(5,0,1,6,11); h(5,0,1,54)
R.append({"updateSheetProperties":{"properties":{"sheetId":5,"gridProperties":{"frozenRowCount":1}},"fields":"gridProperties.frozenRowCount"}})
al(5,1,14,0,1,"CENTER"); al(5,1,14,1,5,"RIGHT")
nf(5,1,14,1,3,"NUMBER","#,##0"); nf(5,1,14,3,4,"PERCENT","0.0%"); nf(5,1,14,4,5,"NUMBER","0.0")
R.append({"repeatCell":{"range":gr(5,13,14,0,5),"cell":{"userEnteredFormat":{"textFormat":{"bold":True},"backgroundColorStyle":{"rgbColor":rgb("#EEEEEE")}}},"fields":"userEnteredFormat.textFormat,userEnteredFormat.backgroundColorStyle"}})
bd(5,0,14,0,5); bd(5,0,26,6,11)
nf(5,1,26,7,9,"NUMBER","#,##0"); nf(5,1,26,9,10,"PERCENT","0.0%"); nf(5,1,26,10,11,"NUMBER","0.0"); al(5,1,26,7,11,"RIGHT")
note(5,15,5); h(5,15,16,96)
w(5,0,1,90); w(5,1,4,90); w(5,4,5,140); w(5,5,6,24); w(5,6,7,240); w(5,7,11,90)
R.append({"addChart":{"chart":{"spec":{"title":"旧ドメインのロボット関連ページ：月ごとのクリック数","basicChart":{"chartType":"COLUMN","legendPosition":"NO_LEGEND","headerCount":1,
 "domains":[{"domain":{"sourceRange":{"sources":[gr(5,0,13,0,1)]}}}],
 "series":[{"series":{"sourceRange":{"sources":[gr(5,0,13,1,2)]}},"targetAxis":"LEFT_AXIS","dataLabel":{"type":"DATA"}}]}},
 "position":{"overlayPosition":{"anchorCell":{"sheetId":5,"rowIndex":17,"columnIndex":0},"widthPixels":620,"heightPixels":320}}}}})
# pages tab 6
body(6); hdr(6,0,1,0,6); h(6,0,1,40)
R.append({"updateSheetProperties":{"properties":{"sheetId":6,"gridProperties":{"frozenRowCount":1}},"fields":"gridProperties.frozenRowCount"}})
nf(6,1,70,2,4,"NUMBER","#,##0"); nf(6,1,70,4,5,"PERCENT","0.0%"); nf(6,1,70,5,6,"NUMBER","0.0"); al(6,1,70,2,6,"RIGHT")
R.append({"repeatCell":{"range":gr(6,69,70,0,6),"cell":{"userEnteredFormat":{"textFormat":{"bold":True},"backgroundColorStyle":{"rgbColor":rgb("#EEEEEE")}}},"fields":"userEnteredFormat.textFormat,userEnteredFormat.backgroundColorStyle"}})
bd(6,0,70,0,6); note(6,71,6); h(6,71,72,60)
w(6,0,2,330); w(6,2,6,90)
R.append({"setBasicFilter":{"filter":{"range":gr(6,0,69,0,6)}}})
R.append({"autoResizeDimensions":{"dimensions":{"sheetId":6,"dimension":"ROWS","startIndex":1,"endIndex":70}}})
R.append({"autoResizeDimensions":{"dimensions":{"sheetId":5,"dimension":"ROWS","startIndex":1,"endIndex":15}}})
R.append({"autoResizeDimensions":{"dimensions":{"sheetId":5,"dimension":"ROWS","startIndex":16,"endIndex":26}}})
# 読み方: insert 2 rows after キーワード (row index 7)
R.append({"insertDimension":{"range":{"sheetId":0,"dimension":"ROWS","startIndex":7,"endIndex":9},"inheritFromBefore":True}})
print(json.dumps(R,ensure_ascii=False))
