ver Feb19thv5

correcting SheetCopilot data and annotations
This commit is contained in:
David Chang
2024-02-19 23:33:51 +08:00
parent ed61404f2b
commit e57d3ef981
3 changed files with 35 additions and 21 deletions

View File

@@ -1,12 +1,13 @@
import logging import logging
import zipfile import zipfile
from typing import Any, TypeVar, Union, Iterable, Optional, Callable from typing import Any, TypeVar, Union, Iterable, Optional, Callable
from typing import Dict, List, Set, Match from typing import Dict, List, Set, Match, Tuple
from urllib.parse import urlparse, urlunparse from urllib.parse import urlparse, urlunparse
import re import re
import functools import functools
import operator import operator
import builtins import builtins
import itertools
import lxml.cssselect import lxml.cssselect
import lxml.etree import lxml.etree
@@ -20,7 +21,7 @@ from openpyxl.worksheet.filters import AutoFilter, SortState
from openpyxl.worksheet.cell_range import MultiCellRange from openpyxl.worksheet.cell_range import MultiCellRange
from openpyxl.worksheet.dimensions import DimensionHolder from openpyxl.worksheet.dimensions import DimensionHolder
from openpyxl.formatting.formatting import ConditionalFormattingList from openpyxl.formatting.formatting import ConditionalFormattingList
#from openpyxl.utils import get_column_letter from openpyxl.utils import coordinate_to_tuple
from openpyxl.cell.cell import Cell from openpyxl.cell.cell import Cell
from openpyxl.styles.differential import DifferentialStyle from openpyxl.styles.differential import DifferentialStyle
from openpyxl.pivot.table import TableDefinition as PivotTableDefinition from openpyxl.pivot.table import TableDefinition as PivotTableDefinition
@@ -176,6 +177,7 @@ def load_charts(xlsx_file: Workbook, sheet_name: str, **options) -> Dict[str, An
except: except:
info["ztitle"] = None info["ztitle"] = None
chart_set[series] = info chart_set[series] = info
logger.debug(".[%s].charts: %s", sheet_name, repr(chart_set))
return chart_set return chart_set
# }}} function load_charts # # }}} function load_charts #
@@ -183,7 +185,7 @@ def load_charts(xlsx_file: Workbook, sheet_name: str, **options) -> Dict[str, An
# name: str # name: str
# show_total, show_empty_row, show_empty_col, show_headers: bool # show_total, show_empty_row, show_empty_col, show_headers: bool
# location: str # location: str
# selection: if the concrete item selection should be checked, a list of list of tuple like (bool, index) will be returned # selection: if the concrete item selection should be checked, a list of set of tuple like (bool, index) will be returned; list will be returned instead of set if "ordered" is specified
# filter: if the filter fields should be checked; fields indices will be return in `filter_fields` item # filter: if the filter fields should be checked; fields indices will be return in `filter_fields` item
# col_fields: indices # col_fields: indices
# row_fields: indices # row_fields: indices
@@ -215,14 +217,27 @@ def load_pivot_tables(xlsx_file: Workbook, sheet_name: str, **options) -> Dict[s
pivot_set: Dict[str, Any] = {} pivot_set: Dict[str, Any] = {}
pivot_props: Set[str] = set(options.get("pivot_props", [])) pivot_props: Set[str] = set(options.get("pivot_props", []))
for pvt in pivots: for pvt in pivots:
name: str = pvt.name raw_selection: List[List[tuple[Optional[bool], int]]] =\
[ [(itm.h, itm.x) for itm in f.items if itm.x is not None]\
for f in pvt.pivotFields
]
raw__selection: List[List[tuple[Optional[bool], int]]] = list(itertools.dropwhile(lambda r: len(r)==0, raw_selection))
left_bias = len(raw_selection)-len(raw__selection)
selection: List[List[tuple[Optional[bool], int]]] = list((itertools.dropwhile(lambda r: len(r)==0, reversed(raw__selection))))[::-1]
right_bias = len(raw__selection)-len(selection)
cache_source: PivotCacheSource = pvt.cache.cacheSource cache_source: PivotCacheSource = pvt.cache.cacheSource
source: str = "{:};{:};{:}".format(cache_source.type, cache_source.worksheetSource.ref, cache_source.worksheetSource.sheet) cell_range1: str
cell_range2: str
cell_range1, cell_range2 = cache_source.worksheetSource.ref.split(":")
cell_range1: Tuple[int, int] = coordinate_to_tuple(cell_range1)
cell_range1 = (cell_range1[0], cell_range1[1]+left_bias)
cell_range2: Tuple[int, int] = coordinate_to_tuple(cell_range2)
cell_range2 = (cell_range2[0], cell_range2[1]-right_bias)
source: str = "{:};{:}:{:};{:}".format(cache_source.type, cell_range1, cell_range2, cache_source.worksheetSource.sheet)
info: Dict[str, Any] = {} info: Dict[str, Any] = {}
#info["source"] =
if "name" in pivot_props: if "name" in pivot_props:
info["name"] = name info["name"] = pvt.name
if "show_total" in pivot_props: if "show_total" in pivot_props:
info["show_total"] = pvt.visualTotals info["show_total"] = pvt.visualTotals
@@ -236,23 +251,22 @@ def load_pivot_tables(xlsx_file: Workbook, sheet_name: str, **options) -> Dict[s
if "location" in pivot_props: if "location" in pivot_props:
info["location"] = pvt.location info["location"] = pvt.location
if "filter" in pivot_props or "selection" in pivot_props: if "filter" in pivot_props or "selection" in pivot_props:
info["selection"] = [ [(itm.h, itm.x) for itm in f.items]\ info["selection"] = selection if "ordered" in pivot_props else list(set(r) for r in selection)
for f in pvt.pivotFields
]
if "filter" in pivot_props: if "filter" in pivot_props:
info["filter_fields"] = set(f.fld for f in pvt.pageFields) info["filter_fields"] = set(f.fld for f in pvt.pageFields)
if "col_fields" in pivot_props: if "col_fields" in pivot_props:
info["col_fields"] = [f.x for f in pvt.colFields] info["col_fields"] = [f.x for f in pvt.colFields]
if "row_fields" in pivot_props: if "row_fields" in pivot_props:
info["row_fields"] = [f.x for f in pvt.rowFields] info["row_fields"] = [f.x-left_bias for f in pvt.rowFields]
if "data_fields" in pivot_props: if "data_fields" in pivot_props:
info["data_fields"] = [ "{:d};{:};{:};{:}".format( f.fld, f.name if "data_fields_name" in pivot_props else "" info["data_fields"] = [ "{:d};{:};{:};{:}".format( f.fld-left_bias, f.name if "data_fields_name" in pivot_props else ""
, f.subtotal, f.showDataAs , f.subtotal, f.showDataAs
)\ )\
for f in pvt.dataFields for f in pvt.dataFields
] ]
pivot_set[source] = info pivot_set[source] = info
logger.debug(".[%s].pivots: %s", sheet_name, repr(pivot_set))
return pivot_set return pivot_set
# }}} function load_pivot_tables # # }}} function load_pivot_tables #

View File

@@ -73,10 +73,10 @@
"rules": [ "rules": [
{ {
"type": "sheet_data", "type": "sheet_data",
"sheet_idx0": 0, "sheet_idx0": "RNSheet2",
"sheet_idx1": "EI0" "sheet_idx1": "ENSheet2"
} }
] ]
} }
} }
} }

View File

@@ -1,7 +1,7 @@
{ {
"id": "163789f0-c895-4a50-8207-17cbdd56ec38", "id": "163789f0-c895-4a50-8207-17cbdd56ec38",
"snapshot": "libreoffice_calc", "snapshot": "libreoffice_calc",
"instruction": "Create a pivot table in a new sheet to calculate the sum of revenue for each product. In this new sheet, plot a horizontal bar chart illustrating the revenue sums in the pivot table. Turn off the legend and then set the chart tile as \"Revenue of each product\".", "instruction": "Create a pivot table in a new sheet (Sheet2) to calculate the sum of revenue for each product. In this new sheet, plot a horizontal bar chart illustrating the revenue sums in the pivot table. Turn off the legend and then set the chart tile as \"Revenue of each product\".",
"source": "SheetCopilot@176", "source": "SheetCopilot@176",
"config": [ "config": [
{ {
@@ -73,8 +73,8 @@
"rules": [ "rules": [
{ {
"type": "chart", "type": "chart",
"sheet_idx0": 0, "sheet_idx0": "RNSheet2",
"sheet_idx1": "EI0", "sheet_idx1": "ENSheet2",
"chart_props": [ "chart_props": [
"type", "type",
"legend", "legend",
@@ -83,8 +83,8 @@
}, },
{ {
"type": "pivot_table", "type": "pivot_table",
"sheet_idx0": 0, "sheet_idx0": "RNSheet2",
"sheet_idx1": "EI0", "sheet_idx1": "ENSheet2",
"pivot_props": [ "pivot_props": [
"col_fields", "col_fields",
"filter", "filter",
@@ -95,4 +95,4 @@
] ]
} }
} }
} }