@v587d/capital-generation 2.28.0 → 2.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -0
- package/README.md +5 -4
- package/THIRD_PARTY_NOTICES.md +16 -0
- package/automation/package.json +1 -1
- package/chart-ui/package.json +1 -1
- package/config/package.json +1 -1
- package/lib/collector-runtime.js +56 -1
- package/lib/data-collector/catalog-taxonomy.d.ts +1 -1
- package/lib/data-collector/catalog-taxonomy.js +33 -0
- package/lib/data-collector/tools.js +7 -7
- package/lib/host-version.d.ts +9 -0
- package/lib/host-version.js +57 -0
- package/lib/python-runtime/errors.d.ts +1 -1
- package/lib/python-runtime/errors.js +2 -0
- package/lib/python-runtime/manager.d.ts +28 -1
- package/lib/python-runtime/manager.js +141 -5
- package/lib/python-runtime/probe.d.ts +24 -0
- package/lib/python-runtime/probe.js +66 -50
- package/lib/runtime-entry.js +8 -0
- package/lib/sources/csindex-http.d.ts +4 -0
- package/lib/sources/csindex-http.js +233 -0
- package/lib/sources/eastmoney-http.js +1504 -1
- package/lib/sources/fuyao-rest.js +168 -8
- package/lib/sources/official-http.d.ts +4 -0
- package/lib/sources/official-http.js +477 -0
- package/lib/sources/python-remote.d.ts +25 -0
- package/lib/sources/python-remote.js +461 -0
- package/lib/sources/sina-http.d.ts +2 -0
- package/lib/sources/sina-http.js +484 -0
- package/lib/sources/tencent-http.js +23 -8
- package/lib/sources/ths-http.d.ts +2 -0
- package/lib/sources/ths-http.js +514 -0
- package/lib/time/tools.js +19 -0
- package/lib/web-retriever/local-fetch.d.ts +1 -1
- package/lib/web-retriever/local-fetch.js +1 -1
- package/lib/web-retriever/sources.d.ts +7 -0
- package/lib/web-retriever/sources.js +46 -21
- package/lib/web-retriever/throttle.d.ts +8 -0
- package/lib/web-retriever/throttle.js +13 -0
- package/lib/web-retriever/tools.js +1 -1
- package/lib/web-retriever/wind-client.d.ts +1 -1
- package/lib/web-retriever/wind-client.js +1 -1
- package/package.json +6 -3
- package/preset/capital-generation/agent.patch.yml +7 -5
- package/preset/capital-generation/skills/capital-data-protocol/SKILL.md +16 -16
- package/python/scripts/baostock_stock_basic.py +129 -0
- package/python/scripts/baostock_valuation_history.py +141 -0
- package/python/scripts/pbc_social_financing.py +291 -0
- package/python/vendor/baostock/LICENSE +19 -0
- package/python/vendor/baostock/__init__.py +42 -0
- package/python/vendor/baostock/common/__init__.py +0 -0
- package/python/vendor/baostock/common/contants.py +189 -0
- package/python/vendor/baostock/common/context.py +4 -0
- package/python/vendor/baostock/corpreport/__init__.py +0 -0
- package/python/vendor/baostock/corpreport/corp_performance.py +202 -0
- package/python/vendor/baostock/data/__init__.py +0 -0
- package/python/vendor/baostock/data/messageheader.py +17 -0
- package/python/vendor/baostock/data/msg.py +21 -0
- package/python/vendor/baostock/data/resultset.py +200 -0
- package/python/vendor/baostock/demo/__init__.py +0 -0
- package/python/vendor/baostock/demo/demo_A_stock_5mk-data.py +34 -0
- package/python/vendor/baostock/demo/demo_A_stock_k_data.py +36 -0
- package/python/vendor/baostock/demo/demo_AllStock.py +27 -0
- package/python/vendor/baostock/demo/demo_Balance_data.py +22 -0
- package/python/vendor/baostock/demo/demo_CashFlow_data.py +22 -0
- package/python/vendor/baostock/demo/demo_Dividend_data.py +35 -0
- package/python/vendor/baostock/demo/demo_Dupont_data.py +25 -0
- package/python/vendor/baostock/demo/demo_Forecast_report.py +24 -0
- package/python/vendor/baostock/demo/demo_Growth_data.py +22 -0
- package/python/vendor/baostock/demo/demo_Index_k_data.py +39 -0
- package/python/vendor/baostock/demo/demo_Operation_data.py +22 -0
- package/python/vendor/baostock/demo/demo_PerformanceExpress_report.py +25 -0
- package/python/vendor/baostock/demo/demo_Profit_data.py +22 -0
- package/python/vendor/baostock/demo/demo_TradeDates.py +27 -0
- package/python/vendor/baostock/demo/demo_adjust_factor_data.py +23 -0
- package/python/vendor/baostock/demo/demo_assignDayData.py +23 -0
- package/python/vendor/baostock/demo/demo_daily_adjust_factor_data.py +28 -0
- package/python/vendor/baostock/demo/demo_daily_k_4_AStock.py +28 -0
- package/python/vendor/baostock/demo/demo_daily_k_4_ETF.py +28 -0
- package/python/vendor/baostock/demo/demo_depositRate_data.py +26 -0
- package/python/vendor/baostock/demo/demo_hs300_stocks.py +26 -0
- package/python/vendor/baostock/demo/demo_loanRate_data.py +26 -0
- package/python/vendor/baostock/demo/demo_moneySupplyDataMonth_data.py +26 -0
- package/python/vendor/baostock/demo/demo_moneySupplyDataYear_data.py +26 -0
- package/python/vendor/baostock/demo/demo_requiredReserveRatio_data.py +26 -0
- package/python/vendor/baostock/demo/demo_set_API_key.py +37 -0
- package/python/vendor/baostock/demo/demo_stockBasic_data.py +27 -0
- package/python/vendor/baostock/demo/demo_stock_Valuation_indicator_data.py +34 -0
- package/python/vendor/baostock/demo/demo_stock_industry.py +26 -0
- package/python/vendor/baostock/demo/demo_sz50stocks.py +26 -0
- package/python/vendor/baostock/demo/demo_zz500_stock.py +26 -0
- package/python/vendor/baostock/evaluation/__init__.py +0 -0
- package/python/vendor/baostock/evaluation/season_index.py +758 -0
- package/python/vendor/baostock/login/__init__.py +0 -0
- package/python/vendor/baostock/login/loginout.py +230 -0
- package/python/vendor/baostock/macroscopic/__init__.py +0 -0
- package/python/vendor/baostock/macroscopic/economic_data.py +723 -0
- package/python/vendor/baostock/metadata/__init__.py +0 -0
- package/python/vendor/baostock/metadata/stock_metadata.py +207 -0
- package/python/vendor/baostock/security/__init__.py +0 -0
- package/python/vendor/baostock/security/history.py +265 -0
- package/python/vendor/baostock/security/sectorinfo.py +1024 -0
- package/python/vendor/baostock/util/__init__.py +0 -0
- package/python/vendor/baostock/util/socketutil.py +97 -0
- package/python/vendor/baostock/util/stringutil.py +83 -0
- package/python/vendor/et_xmlfile/LICENSE +23 -0
- package/python/vendor/et_xmlfile/LICENSE.python +298 -0
- package/python/vendor/et_xmlfile/__init__.py +8 -0
- package/python/vendor/et_xmlfile/incremental_tree.py +917 -0
- package/python/vendor/et_xmlfile/xmlfile.py +158 -0
- package/python/vendor/openpyxl/LICENSE +23 -0
- package/python/vendor/openpyxl/__init__.py +19 -0
- package/python/vendor/openpyxl/_constants.py +13 -0
- package/python/vendor/openpyxl/cell/__init__.py +4 -0
- package/python/vendor/openpyxl/cell/_writer.py +136 -0
- package/python/vendor/openpyxl/cell/cell.py +332 -0
- package/python/vendor/openpyxl/cell/read_only.py +136 -0
- package/python/vendor/openpyxl/cell/rich_text.py +202 -0
- package/python/vendor/openpyxl/cell/text.py +184 -0
- package/python/vendor/openpyxl/chart/_3d.py +105 -0
- package/python/vendor/openpyxl/chart/__init__.py +19 -0
- package/python/vendor/openpyxl/chart/_chart.py +199 -0
- package/python/vendor/openpyxl/chart/area_chart.py +106 -0
- package/python/vendor/openpyxl/chart/axis.py +401 -0
- package/python/vendor/openpyxl/chart/bar_chart.py +144 -0
- package/python/vendor/openpyxl/chart/bubble_chart.py +67 -0
- package/python/vendor/openpyxl/chart/chartspace.py +195 -0
- package/python/vendor/openpyxl/chart/data_source.py +246 -0
- package/python/vendor/openpyxl/chart/descriptors.py +43 -0
- package/python/vendor/openpyxl/chart/error_bar.py +62 -0
- package/python/vendor/openpyxl/chart/label.py +127 -0
- package/python/vendor/openpyxl/chart/layout.py +74 -0
- package/python/vendor/openpyxl/chart/legend.py +75 -0
- package/python/vendor/openpyxl/chart/line_chart.py +129 -0
- package/python/vendor/openpyxl/chart/marker.py +90 -0
- package/python/vendor/openpyxl/chart/picture.py +35 -0
- package/python/vendor/openpyxl/chart/pie_chart.py +177 -0
- package/python/vendor/openpyxl/chart/pivot.py +65 -0
- package/python/vendor/openpyxl/chart/plotarea.py +162 -0
- package/python/vendor/openpyxl/chart/print_settings.py +57 -0
- package/python/vendor/openpyxl/chart/radar_chart.py +55 -0
- package/python/vendor/openpyxl/chart/reader.py +32 -0
- package/python/vendor/openpyxl/chart/reference.py +124 -0
- package/python/vendor/openpyxl/chart/scatter_chart.py +53 -0
- package/python/vendor/openpyxl/chart/series.py +197 -0
- package/python/vendor/openpyxl/chart/series_factory.py +41 -0
- package/python/vendor/openpyxl/chart/shapes.py +89 -0
- package/python/vendor/openpyxl/chart/stock_chart.py +54 -0
- package/python/vendor/openpyxl/chart/surface_chart.py +119 -0
- package/python/vendor/openpyxl/chart/text.py +78 -0
- package/python/vendor/openpyxl/chart/title.py +76 -0
- package/python/vendor/openpyxl/chart/trendline.py +98 -0
- package/python/vendor/openpyxl/chart/updown_bars.py +31 -0
- package/python/vendor/openpyxl/chartsheet/__init__.py +3 -0
- package/python/vendor/openpyxl/chartsheet/chartsheet.py +107 -0
- package/python/vendor/openpyxl/chartsheet/custom.py +61 -0
- package/python/vendor/openpyxl/chartsheet/properties.py +28 -0
- package/python/vendor/openpyxl/chartsheet/protection.py +41 -0
- package/python/vendor/openpyxl/chartsheet/publish.py +58 -0
- package/python/vendor/openpyxl/chartsheet/relation.py +97 -0
- package/python/vendor/openpyxl/chartsheet/views.py +51 -0
- package/python/vendor/openpyxl/comments/__init__.py +4 -0
- package/python/vendor/openpyxl/comments/author.py +21 -0
- package/python/vendor/openpyxl/comments/comment_sheet.py +211 -0
- package/python/vendor/openpyxl/comments/comments.py +62 -0
- package/python/vendor/openpyxl/comments/shape_writer.py +112 -0
- package/python/vendor/openpyxl/compat/__init__.py +54 -0
- package/python/vendor/openpyxl/compat/abc.py +8 -0
- package/python/vendor/openpyxl/compat/numbers.py +43 -0
- package/python/vendor/openpyxl/compat/product.py +17 -0
- package/python/vendor/openpyxl/compat/singleton.py +40 -0
- package/python/vendor/openpyxl/compat/strings.py +25 -0
- package/python/vendor/openpyxl/descriptors/__init__.py +58 -0
- package/python/vendor/openpyxl/descriptors/base.py +272 -0
- package/python/vendor/openpyxl/descriptors/container.py +41 -0
- package/python/vendor/openpyxl/descriptors/excel.py +112 -0
- package/python/vendor/openpyxl/descriptors/namespace.py +12 -0
- package/python/vendor/openpyxl/descriptors/nested.py +129 -0
- package/python/vendor/openpyxl/descriptors/sequence.py +136 -0
- package/python/vendor/openpyxl/descriptors/serialisable.py +240 -0
- package/python/vendor/openpyxl/descriptors/slots.py +18 -0
- package/python/vendor/openpyxl/drawing/__init__.py +4 -0
- package/python/vendor/openpyxl/drawing/colors.py +435 -0
- package/python/vendor/openpyxl/drawing/connector.py +144 -0
- package/python/vendor/openpyxl/drawing/drawing.py +92 -0
- package/python/vendor/openpyxl/drawing/effect.py +407 -0
- package/python/vendor/openpyxl/drawing/fill.py +425 -0
- package/python/vendor/openpyxl/drawing/geometry.py +584 -0
- package/python/vendor/openpyxl/drawing/graphic.py +177 -0
- package/python/vendor/openpyxl/drawing/image.py +65 -0
- package/python/vendor/openpyxl/drawing/line.py +144 -0
- package/python/vendor/openpyxl/drawing/picture.py +144 -0
- package/python/vendor/openpyxl/drawing/properties.py +174 -0
- package/python/vendor/openpyxl/drawing/relation.py +17 -0
- package/python/vendor/openpyxl/drawing/spreadsheet_drawing.py +382 -0
- package/python/vendor/openpyxl/drawing/text.py +717 -0
- package/python/vendor/openpyxl/drawing/xdr.py +33 -0
- package/python/vendor/openpyxl/formatting/__init__.py +3 -0
- package/python/vendor/openpyxl/formatting/formatting.py +114 -0
- package/python/vendor/openpyxl/formatting/rule.py +291 -0
- package/python/vendor/openpyxl/formula/__init__.py +3 -0
- package/python/vendor/openpyxl/formula/tokenizer.py +446 -0
- package/python/vendor/openpyxl/formula/translate.py +166 -0
- package/python/vendor/openpyxl/packaging/__init__.py +3 -0
- package/python/vendor/openpyxl/packaging/core.py +115 -0
- package/python/vendor/openpyxl/packaging/custom.py +289 -0
- package/python/vendor/openpyxl/packaging/extended.py +137 -0
- package/python/vendor/openpyxl/packaging/interface.py +56 -0
- package/python/vendor/openpyxl/packaging/manifest.py +194 -0
- package/python/vendor/openpyxl/packaging/relationship.py +158 -0
- package/python/vendor/openpyxl/packaging/workbook.py +185 -0
- package/python/vendor/openpyxl/pivot/__init__.py +1 -0
- package/python/vendor/openpyxl/pivot/cache.py +965 -0
- package/python/vendor/openpyxl/pivot/fields.py +326 -0
- package/python/vendor/openpyxl/pivot/record.py +111 -0
- package/python/vendor/openpyxl/pivot/table.py +1261 -0
- package/python/vendor/openpyxl/reader/__init__.py +1 -0
- package/python/vendor/openpyxl/reader/drawings.py +71 -0
- package/python/vendor/openpyxl/reader/excel.py +349 -0
- package/python/vendor/openpyxl/reader/strings.py +44 -0
- package/python/vendor/openpyxl/reader/workbook.py +133 -0
- package/python/vendor/openpyxl/styles/__init__.py +11 -0
- package/python/vendor/openpyxl/styles/alignment.py +62 -0
- package/python/vendor/openpyxl/styles/borders.py +103 -0
- package/python/vendor/openpyxl/styles/builtins.py +1397 -0
- package/python/vendor/openpyxl/styles/cell_style.py +206 -0
- package/python/vendor/openpyxl/styles/colors.py +172 -0
- package/python/vendor/openpyxl/styles/differential.py +95 -0
- package/python/vendor/openpyxl/styles/fills.py +224 -0
- package/python/vendor/openpyxl/styles/fonts.py +113 -0
- package/python/vendor/openpyxl/styles/named_styles.py +282 -0
- package/python/vendor/openpyxl/styles/numbers.py +200 -0
- package/python/vendor/openpyxl/styles/protection.py +17 -0
- package/python/vendor/openpyxl/styles/proxy.py +62 -0
- package/python/vendor/openpyxl/styles/styleable.py +151 -0
- package/python/vendor/openpyxl/styles/stylesheet.py +274 -0
- package/python/vendor/openpyxl/styles/table.py +94 -0
- package/python/vendor/openpyxl/utils/__init__.py +17 -0
- package/python/vendor/openpyxl/utils/bound_dictionary.py +26 -0
- package/python/vendor/openpyxl/utils/cell.py +240 -0
- package/python/vendor/openpyxl/utils/dataframe.py +87 -0
- package/python/vendor/openpyxl/utils/datetime.py +140 -0
- package/python/vendor/openpyxl/utils/escape.py +43 -0
- package/python/vendor/openpyxl/utils/exceptions.py +34 -0
- package/python/vendor/openpyxl/utils/formulas.py +24 -0
- package/python/vendor/openpyxl/utils/indexed_list.py +49 -0
- package/python/vendor/openpyxl/utils/inference.py +60 -0
- package/python/vendor/openpyxl/utils/protection.py +22 -0
- package/python/vendor/openpyxl/utils/units.py +108 -0
- package/python/vendor/openpyxl/workbook/__init__.py +4 -0
- package/python/vendor/openpyxl/workbook/_writer.py +197 -0
- package/python/vendor/openpyxl/workbook/child.py +166 -0
- package/python/vendor/openpyxl/workbook/defined_name.py +189 -0
- package/python/vendor/openpyxl/workbook/external_link/__init__.py +3 -0
- package/python/vendor/openpyxl/workbook/external_link/external.py +190 -0
- package/python/vendor/openpyxl/workbook/external_reference.py +18 -0
- package/python/vendor/openpyxl/workbook/function_group.py +36 -0
- package/python/vendor/openpyxl/workbook/properties.py +151 -0
- package/python/vendor/openpyxl/workbook/protection.py +163 -0
- package/python/vendor/openpyxl/workbook/smart_tags.py +56 -0
- package/python/vendor/openpyxl/workbook/views.py +155 -0
- package/python/vendor/openpyxl/workbook/web.py +98 -0
- package/python/vendor/openpyxl/workbook/workbook.py +438 -0
- package/python/vendor/openpyxl/worksheet/__init__.py +1 -0
- package/python/vendor/openpyxl/worksheet/_read_only.py +190 -0
- package/python/vendor/openpyxl/worksheet/_reader.py +472 -0
- package/python/vendor/openpyxl/worksheet/_write_only.py +160 -0
- package/python/vendor/openpyxl/worksheet/_writer.py +390 -0
- package/python/vendor/openpyxl/worksheet/cell_range.py +512 -0
- package/python/vendor/openpyxl/worksheet/cell_watch.py +34 -0
- package/python/vendor/openpyxl/worksheet/controls.py +107 -0
- package/python/vendor/openpyxl/worksheet/copier.py +70 -0
- package/python/vendor/openpyxl/worksheet/custom.py +35 -0
- package/python/vendor/openpyxl/worksheet/datavalidation.py +202 -0
- package/python/vendor/openpyxl/worksheet/dimensions.py +306 -0
- package/python/vendor/openpyxl/worksheet/drawing.py +14 -0
- package/python/vendor/openpyxl/worksheet/errors.py +93 -0
- package/python/vendor/openpyxl/worksheet/filters.py +486 -0
- package/python/vendor/openpyxl/worksheet/formula.py +51 -0
- package/python/vendor/openpyxl/worksheet/header_footer.py +270 -0
- package/python/vendor/openpyxl/worksheet/hyperlink.py +46 -0
- package/python/vendor/openpyxl/worksheet/merge.py +141 -0
- package/python/vendor/openpyxl/worksheet/ole.py +133 -0
- package/python/vendor/openpyxl/worksheet/page.py +174 -0
- package/python/vendor/openpyxl/worksheet/pagebreak.py +94 -0
- package/python/vendor/openpyxl/worksheet/picture.py +8 -0
- package/python/vendor/openpyxl/worksheet/print_settings.py +184 -0
- package/python/vendor/openpyxl/worksheet/properties.py +97 -0
- package/python/vendor/openpyxl/worksheet/protection.py +120 -0
- package/python/vendor/openpyxl/worksheet/related.py +17 -0
- package/python/vendor/openpyxl/worksheet/scenario.py +105 -0
- package/python/vendor/openpyxl/worksheet/smart_tag.py +78 -0
- package/python/vendor/openpyxl/worksheet/table.py +385 -0
- package/python/vendor/openpyxl/worksheet/views.py +155 -0
- package/python/vendor/openpyxl/worksheet/worksheet.py +907 -0
- package/python/vendor/openpyxl/writer/__init__.py +1 -0
- package/python/vendor/openpyxl/writer/excel.py +295 -0
- package/python/vendor/openpyxl/writer/theme.py +291 -0
- package/python/vendor/openpyxl/xml/__init__.py +42 -0
- package/python/vendor/openpyxl/xml/constants.py +129 -0
- package/python/vendor/openpyxl/xml/functions.py +87 -0
- package/watchlist/package.json +1 -1
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module contains a tokenizer for Excel formulae.
|
|
3
|
+
|
|
4
|
+
The tokenizer is based on the Javascript tokenizer found at
|
|
5
|
+
http://ewbi.blogs.com/develops/2004/12/excel_formula_p.html written by Eric
|
|
6
|
+
Bachtal
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import re
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TokenizerError(Exception):
|
|
13
|
+
"""Base class for all Tokenizer errors."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Tokenizer:
|
|
17
|
+
|
|
18
|
+
"""
|
|
19
|
+
A tokenizer for Excel worksheet formulae.
|
|
20
|
+
|
|
21
|
+
Converts a str string representing an Excel formula (in A1 notation)
|
|
22
|
+
into a sequence of `Token` objects.
|
|
23
|
+
|
|
24
|
+
`formula`: The str string to tokenize
|
|
25
|
+
|
|
26
|
+
Tokenizer defines a method `._parse()` to parse the formula into tokens,
|
|
27
|
+
which can then be accessed through the `.items` attribute.
|
|
28
|
+
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
SN_RE = re.compile("^[1-9](\\.[0-9]+)?[Ee]$") # Scientific notation
|
|
32
|
+
WSPACE_RE = re.compile(r"[ \n]+")
|
|
33
|
+
STRING_REGEXES = {
|
|
34
|
+
# Inside a string, all characters are treated as literals, except for
|
|
35
|
+
# the quote character used to start the string. That character, when
|
|
36
|
+
# doubled is treated as a single character in the string. If an
|
|
37
|
+
# unmatched quote appears, the string is terminated.
|
|
38
|
+
'"': re.compile('"(?:[^"]*"")*[^"]*"(?!")'),
|
|
39
|
+
"'": re.compile("'(?:[^']*'')*[^']*'(?!')"),
|
|
40
|
+
}
|
|
41
|
+
ERROR_CODES = ("#NULL!", "#DIV/0!", "#VALUE!", "#REF!", "#NAME?",
|
|
42
|
+
"#NUM!", "#N/A", "#GETTING_DATA")
|
|
43
|
+
TOKEN_ENDERS = ',;}) +-*/^&=><%' # Each of these characters, marks the
|
|
44
|
+
# end of an operand token
|
|
45
|
+
|
|
46
|
+
def __init__(self, formula):
|
|
47
|
+
self.formula = formula
|
|
48
|
+
self.items = []
|
|
49
|
+
self.token_stack = [] # Used to keep track of arrays, functions, and
|
|
50
|
+
# parentheses
|
|
51
|
+
self.offset = 0 # How many chars have we read
|
|
52
|
+
self.token = [] # Used to build up token values char by char
|
|
53
|
+
self._parse()
|
|
54
|
+
|
|
55
|
+
def _parse(self):
|
|
56
|
+
"""Populate self.items with the tokens from the formula."""
|
|
57
|
+
if self.offset:
|
|
58
|
+
return # Already parsed!
|
|
59
|
+
if not self.formula:
|
|
60
|
+
return
|
|
61
|
+
elif self.formula[0] == '=':
|
|
62
|
+
self.offset += 1
|
|
63
|
+
else:
|
|
64
|
+
self.items.append(Token(self.formula, Token.LITERAL))
|
|
65
|
+
return
|
|
66
|
+
consumers = (
|
|
67
|
+
('"\'', self._parse_string),
|
|
68
|
+
('[', self._parse_brackets),
|
|
69
|
+
('#', self._parse_error),
|
|
70
|
+
(' ', self._parse_whitespace),
|
|
71
|
+
('\n', self._parse_whitespace),
|
|
72
|
+
('+-*/^&=><%', self._parse_operator),
|
|
73
|
+
('{(', self._parse_opener),
|
|
74
|
+
(')}', self._parse_closer),
|
|
75
|
+
(';,', self._parse_separator),
|
|
76
|
+
)
|
|
77
|
+
dispatcher = {} # maps chars to the specific parsing function
|
|
78
|
+
for chars, consumer in consumers:
|
|
79
|
+
dispatcher.update(dict.fromkeys(chars, consumer))
|
|
80
|
+
while self.offset < len(self.formula):
|
|
81
|
+
if self.check_scientific_notation(): # May consume one character
|
|
82
|
+
continue
|
|
83
|
+
curr_char = self.formula[self.offset]
|
|
84
|
+
if curr_char in self.TOKEN_ENDERS:
|
|
85
|
+
self.save_token()
|
|
86
|
+
if curr_char in dispatcher:
|
|
87
|
+
self.offset += dispatcher[curr_char]()
|
|
88
|
+
else:
|
|
89
|
+
# TODO: this can probably be sped up using a regex to get to
|
|
90
|
+
# the next interesting character
|
|
91
|
+
self.token.append(curr_char)
|
|
92
|
+
self.offset += 1
|
|
93
|
+
self.save_token()
|
|
94
|
+
|
|
95
|
+
def _parse_string(self):
|
|
96
|
+
"""
|
|
97
|
+
Parse a "-delimited string or '-delimited link.
|
|
98
|
+
|
|
99
|
+
The offset must be pointing to either a single quote ("'") or double
|
|
100
|
+
quote ('"') character. The strings are parsed according to Excel
|
|
101
|
+
rules where to escape the delimiter you just double it up. E.g.,
|
|
102
|
+
"abc""def" in Excel is parsed as 'abc"def' in Python.
|
|
103
|
+
|
|
104
|
+
Returns the number of characters matched. (Does not update
|
|
105
|
+
self.offset)
|
|
106
|
+
|
|
107
|
+
"""
|
|
108
|
+
self.assert_empty_token(can_follow=':')
|
|
109
|
+
delim = self.formula[self.offset]
|
|
110
|
+
assert delim in ('"', "'")
|
|
111
|
+
regex = self.STRING_REGEXES[delim]
|
|
112
|
+
match = regex.match(self.formula[self.offset:])
|
|
113
|
+
if match is None:
|
|
114
|
+
subtype = "string" if delim == '"' else 'link'
|
|
115
|
+
raise TokenizerError(f"Reached end of formula while parsing {subtype} in {self.formula}")
|
|
116
|
+
match = match.group(0)
|
|
117
|
+
if delim == '"':
|
|
118
|
+
self.items.append(Token.make_operand(match))
|
|
119
|
+
else:
|
|
120
|
+
self.token.append(match)
|
|
121
|
+
return len(match)
|
|
122
|
+
|
|
123
|
+
def _parse_brackets(self):
|
|
124
|
+
"""
|
|
125
|
+
Consume all the text between square brackets [].
|
|
126
|
+
|
|
127
|
+
Returns the number of characters matched. (Does not update
|
|
128
|
+
self.offset)
|
|
129
|
+
|
|
130
|
+
"""
|
|
131
|
+
assert self.formula[self.offset] == '['
|
|
132
|
+
lefts = [(t.start(), 1) for t in
|
|
133
|
+
re.finditer(r"\[", self.formula[self.offset:])]
|
|
134
|
+
rights = [(t.start(), -1) for t in
|
|
135
|
+
re.finditer(r"\]", self.formula[self.offset:])]
|
|
136
|
+
|
|
137
|
+
open_count = 0
|
|
138
|
+
for idx, open_close in sorted(lefts + rights):
|
|
139
|
+
open_count += open_close
|
|
140
|
+
if open_count == 0:
|
|
141
|
+
outer_right = idx + 1
|
|
142
|
+
self.token.append(
|
|
143
|
+
self.formula[self.offset:self.offset + outer_right])
|
|
144
|
+
return outer_right
|
|
145
|
+
|
|
146
|
+
raise TokenizerError(f"Encountered unmatched '[' in {self.formula}")
|
|
147
|
+
|
|
148
|
+
def _parse_error(self):
|
|
149
|
+
"""
|
|
150
|
+
Consume the text following a '#' as an error.
|
|
151
|
+
|
|
152
|
+
Looks for a match in self.ERROR_CODES and returns the number of
|
|
153
|
+
characters matched. (Does not update self.offset)
|
|
154
|
+
|
|
155
|
+
"""
|
|
156
|
+
self.assert_empty_token(can_follow='!')
|
|
157
|
+
assert self.formula[self.offset] == '#'
|
|
158
|
+
subformula = self.formula[self.offset:]
|
|
159
|
+
for err in self.ERROR_CODES:
|
|
160
|
+
if subformula.startswith(err):
|
|
161
|
+
self.items.append(Token.make_operand(''.join(self.token) + err))
|
|
162
|
+
del self.token[:]
|
|
163
|
+
return len(err)
|
|
164
|
+
raise TokenizerError(f"Invalid error code at position {self.offset} in '{self.formula}'")
|
|
165
|
+
|
|
166
|
+
def _parse_whitespace(self):
|
|
167
|
+
"""
|
|
168
|
+
Consume a string of consecutive spaces.
|
|
169
|
+
|
|
170
|
+
Returns the number of spaces found. (Does not update self.offset).
|
|
171
|
+
|
|
172
|
+
"""
|
|
173
|
+
assert self.formula[self.offset] in (' ', '\n')
|
|
174
|
+
self.items.append(Token(self.formula[self.offset], Token.WSPACE))
|
|
175
|
+
return self.WSPACE_RE.match(self.formula[self.offset:]).end()
|
|
176
|
+
|
|
177
|
+
def _parse_operator(self):
|
|
178
|
+
"""
|
|
179
|
+
Consume the characters constituting an operator.
|
|
180
|
+
|
|
181
|
+
Returns the number of characters consumed. (Does not update
|
|
182
|
+
self.offset)
|
|
183
|
+
|
|
184
|
+
"""
|
|
185
|
+
if self.formula[self.offset:self.offset + 2] in ('>=', '<=', '<>'):
|
|
186
|
+
self.items.append(Token(
|
|
187
|
+
self.formula[self.offset:self.offset + 2],
|
|
188
|
+
Token.OP_IN
|
|
189
|
+
))
|
|
190
|
+
return 2
|
|
191
|
+
curr_char = self.formula[self.offset] # guaranteed to be 1 char
|
|
192
|
+
assert curr_char in '%*/^&=><+-'
|
|
193
|
+
if curr_char == '%':
|
|
194
|
+
token = Token('%', Token.OP_POST)
|
|
195
|
+
elif curr_char in "*/^&=><":
|
|
196
|
+
token = Token(curr_char, Token.OP_IN)
|
|
197
|
+
# From here on, curr_char is guaranteed to be in '+-'
|
|
198
|
+
elif not self.items:
|
|
199
|
+
token = Token(curr_char, Token.OP_PRE)
|
|
200
|
+
else:
|
|
201
|
+
prev = next((i for i in reversed(self.items)
|
|
202
|
+
if i.type != Token.WSPACE), None)
|
|
203
|
+
is_infix = prev and (
|
|
204
|
+
prev.subtype == Token.CLOSE
|
|
205
|
+
or prev.type == Token.OP_POST
|
|
206
|
+
or prev.type == Token.OPERAND
|
|
207
|
+
)
|
|
208
|
+
if is_infix:
|
|
209
|
+
token = Token(curr_char, Token.OP_IN)
|
|
210
|
+
else:
|
|
211
|
+
token = Token(curr_char, Token.OP_PRE)
|
|
212
|
+
self.items.append(token)
|
|
213
|
+
return 1
|
|
214
|
+
|
|
215
|
+
def _parse_opener(self):
|
|
216
|
+
"""
|
|
217
|
+
Consumes a ( or { character.
|
|
218
|
+
|
|
219
|
+
Returns the number of characters consumed. (Does not update
|
|
220
|
+
self.offset)
|
|
221
|
+
|
|
222
|
+
"""
|
|
223
|
+
assert self.formula[self.offset] in ('(', '{')
|
|
224
|
+
if self.formula[self.offset] == '{':
|
|
225
|
+
self.assert_empty_token()
|
|
226
|
+
token = Token.make_subexp("{")
|
|
227
|
+
elif self.token:
|
|
228
|
+
token_value = "".join(self.token) + '('
|
|
229
|
+
del self.token[:]
|
|
230
|
+
token = Token.make_subexp(token_value)
|
|
231
|
+
else:
|
|
232
|
+
token = Token.make_subexp("(")
|
|
233
|
+
self.items.append(token)
|
|
234
|
+
self.token_stack.append(token)
|
|
235
|
+
return 1
|
|
236
|
+
|
|
237
|
+
def _parse_closer(self):
|
|
238
|
+
"""
|
|
239
|
+
Consumes a } or ) character.
|
|
240
|
+
|
|
241
|
+
Returns the number of characters consumed. (Does not update
|
|
242
|
+
self.offset)
|
|
243
|
+
|
|
244
|
+
"""
|
|
245
|
+
assert self.formula[self.offset] in (')', '}')
|
|
246
|
+
token = self.token_stack.pop().get_closer()
|
|
247
|
+
if token.value != self.formula[self.offset]:
|
|
248
|
+
raise TokenizerError(
|
|
249
|
+
"Mismatched ( and { pair in '%s'" % self.formula)
|
|
250
|
+
self.items.append(token)
|
|
251
|
+
return 1
|
|
252
|
+
|
|
253
|
+
def _parse_separator(self):
|
|
254
|
+
"""
|
|
255
|
+
Consumes a ; or , character.
|
|
256
|
+
|
|
257
|
+
Returns the number of characters consumed. (Does not update
|
|
258
|
+
self.offset)
|
|
259
|
+
|
|
260
|
+
"""
|
|
261
|
+
curr_char = self.formula[self.offset]
|
|
262
|
+
assert curr_char in (';', ',')
|
|
263
|
+
if curr_char == ';':
|
|
264
|
+
token = Token.make_separator(";")
|
|
265
|
+
else:
|
|
266
|
+
try:
|
|
267
|
+
top_type = self.token_stack[-1].type
|
|
268
|
+
except IndexError:
|
|
269
|
+
token = Token(",", Token.OP_IN) # Range Union operator
|
|
270
|
+
else:
|
|
271
|
+
if top_type == Token.PAREN:
|
|
272
|
+
token = Token(",", Token.OP_IN) # Range Union operator
|
|
273
|
+
else:
|
|
274
|
+
token = Token.make_separator(",")
|
|
275
|
+
self.items.append(token)
|
|
276
|
+
return 1
|
|
277
|
+
|
|
278
|
+
def check_scientific_notation(self):
|
|
279
|
+
"""
|
|
280
|
+
Consumes a + or - character if part of a number in sci. notation.
|
|
281
|
+
|
|
282
|
+
Returns True if the character was consumed and self.offset was
|
|
283
|
+
updated, False otherwise.
|
|
284
|
+
|
|
285
|
+
"""
|
|
286
|
+
curr_char = self.formula[self.offset]
|
|
287
|
+
if (curr_char in '+-'
|
|
288
|
+
and len(self.token) >= 1
|
|
289
|
+
and self.SN_RE.match("".join(self.token))):
|
|
290
|
+
self.token.append(curr_char)
|
|
291
|
+
self.offset += 1
|
|
292
|
+
return True
|
|
293
|
+
return False
|
|
294
|
+
|
|
295
|
+
def assert_empty_token(self, can_follow=()):
|
|
296
|
+
"""
|
|
297
|
+
Ensure that there's no token currently being parsed.
|
|
298
|
+
|
|
299
|
+
Or if there is a token being parsed, it must end with a character in
|
|
300
|
+
can_follow.
|
|
301
|
+
|
|
302
|
+
If there are unconsumed token contents, it means we hit an unexpected
|
|
303
|
+
token transition. In this case, we raise a TokenizerError
|
|
304
|
+
|
|
305
|
+
"""
|
|
306
|
+
if self.token and self.token[-1] not in can_follow:
|
|
307
|
+
raise TokenizerError(f"Unexpected character at position {self.offset} in '{self.formula}'")
|
|
308
|
+
|
|
309
|
+
def save_token(self):
|
|
310
|
+
"""If there's a token being parsed, add it to the item list."""
|
|
311
|
+
if self.token:
|
|
312
|
+
self.items.append(Token.make_operand("".join(self.token)))
|
|
313
|
+
del self.token[:]
|
|
314
|
+
|
|
315
|
+
def render(self):
|
|
316
|
+
"""Convert the parsed tokens back to a string."""
|
|
317
|
+
if not self.items:
|
|
318
|
+
return ""
|
|
319
|
+
elif self.items[0].type == Token.LITERAL:
|
|
320
|
+
return self.items[0].value
|
|
321
|
+
return "=" + "".join(token.value for token in self.items)
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
class Token:
|
|
325
|
+
|
|
326
|
+
"""
|
|
327
|
+
A token in an Excel formula.
|
|
328
|
+
|
|
329
|
+
Tokens have three attributes:
|
|
330
|
+
|
|
331
|
+
* `value`: The string value parsed that led to this token
|
|
332
|
+
* `type`: A string identifying the type of token
|
|
333
|
+
* `subtype`: A string identifying subtype of the token (optional, and
|
|
334
|
+
defaults to "")
|
|
335
|
+
|
|
336
|
+
"""
|
|
337
|
+
|
|
338
|
+
__slots__ = ['value', 'type', 'subtype']
|
|
339
|
+
|
|
340
|
+
LITERAL = "LITERAL"
|
|
341
|
+
OPERAND = "OPERAND"
|
|
342
|
+
FUNC = "FUNC"
|
|
343
|
+
ARRAY = "ARRAY"
|
|
344
|
+
PAREN = "PAREN"
|
|
345
|
+
SEP = "SEP"
|
|
346
|
+
OP_PRE = "OPERATOR-PREFIX"
|
|
347
|
+
OP_IN = "OPERATOR-INFIX"
|
|
348
|
+
OP_POST = "OPERATOR-POSTFIX"
|
|
349
|
+
WSPACE = "WHITE-SPACE"
|
|
350
|
+
|
|
351
|
+
def __init__(self, value, type_, subtype=""):
|
|
352
|
+
self.value = value
|
|
353
|
+
self.type = type_
|
|
354
|
+
self.subtype = subtype
|
|
355
|
+
|
|
356
|
+
# Literal operands:
|
|
357
|
+
#
|
|
358
|
+
# Literal operands are always of type 'OPERAND' and can be of subtype
|
|
359
|
+
# 'TEXT' (for text strings), 'NUMBER' (for all numeric types), 'LOGICAL'
|
|
360
|
+
# (for TRUE and FALSE), 'ERROR' (for literal error values), or 'RANGE'
|
|
361
|
+
# (for all range references).
|
|
362
|
+
|
|
363
|
+
TEXT = 'TEXT'
|
|
364
|
+
NUMBER = 'NUMBER'
|
|
365
|
+
LOGICAL = 'LOGICAL'
|
|
366
|
+
ERROR = 'ERROR'
|
|
367
|
+
RANGE = 'RANGE'
|
|
368
|
+
|
|
369
|
+
def __repr__(self):
|
|
370
|
+
return u"{0} {1} {2}:".format(self.type, self.subtype, self.value)
|
|
371
|
+
|
|
372
|
+
@classmethod
|
|
373
|
+
def make_operand(cls, value):
|
|
374
|
+
"""Create an operand token."""
|
|
375
|
+
if value.startswith('"'):
|
|
376
|
+
subtype = cls.TEXT
|
|
377
|
+
elif value.startswith('#'):
|
|
378
|
+
subtype = cls.ERROR
|
|
379
|
+
elif value in ('TRUE', 'FALSE'):
|
|
380
|
+
subtype = cls.LOGICAL
|
|
381
|
+
else:
|
|
382
|
+
try:
|
|
383
|
+
float(value)
|
|
384
|
+
subtype = cls.NUMBER
|
|
385
|
+
except ValueError:
|
|
386
|
+
subtype = cls.RANGE
|
|
387
|
+
return cls(value, cls.OPERAND, subtype)
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
# Subexpresssions
|
|
391
|
+
#
|
|
392
|
+
# There are 3 types of `Subexpressions`: functions, array literals, and
|
|
393
|
+
# parentheticals. Subexpressions have 'OPEN' and 'CLOSE' tokens. 'OPEN'
|
|
394
|
+
# is used when parsing the initial expression token (i.e., '(' or '{')
|
|
395
|
+
# and 'CLOSE' is used when parsing the closing expression token ('}' or
|
|
396
|
+
# ')').
|
|
397
|
+
|
|
398
|
+
OPEN = "OPEN"
|
|
399
|
+
CLOSE = "CLOSE"
|
|
400
|
+
|
|
401
|
+
@classmethod
|
|
402
|
+
def make_subexp(cls, value, func=False):
|
|
403
|
+
"""
|
|
404
|
+
Create a subexpression token.
|
|
405
|
+
|
|
406
|
+
`value`: The value of the token
|
|
407
|
+
`func`: If True, force the token to be of type FUNC
|
|
408
|
+
|
|
409
|
+
"""
|
|
410
|
+
assert value[-1] in ('{', '}', '(', ')')
|
|
411
|
+
if func:
|
|
412
|
+
assert re.match('.+\\(|\\)', value)
|
|
413
|
+
type_ = Token.FUNC
|
|
414
|
+
elif value in '{}':
|
|
415
|
+
type_ = Token.ARRAY
|
|
416
|
+
elif value in '()':
|
|
417
|
+
type_ = Token.PAREN
|
|
418
|
+
else:
|
|
419
|
+
type_ = Token.FUNC
|
|
420
|
+
subtype = cls.CLOSE if value in ')}' else cls.OPEN
|
|
421
|
+
return cls(value, type_, subtype)
|
|
422
|
+
|
|
423
|
+
def get_closer(self):
|
|
424
|
+
"""Return a closing token that matches this token's type."""
|
|
425
|
+
assert self.type in (self.FUNC, self.ARRAY, self.PAREN)
|
|
426
|
+
assert self.subtype == self.OPEN
|
|
427
|
+
value = "}" if self.type == self.ARRAY else ")"
|
|
428
|
+
return self.make_subexp(value, func=self.type == self.FUNC)
|
|
429
|
+
|
|
430
|
+
# Separator tokens
|
|
431
|
+
#
|
|
432
|
+
# Argument separators always have type 'SEP' and can have one of two
|
|
433
|
+
# subtypes: 'ARG', 'ROW'. 'ARG' is used for the ',' token, when used to
|
|
434
|
+
# delimit either function arguments or array elements. 'ROW' is used for
|
|
435
|
+
# the ';' token, which is always used to delimit rows in an array
|
|
436
|
+
# literal.
|
|
437
|
+
|
|
438
|
+
ARG = "ARG"
|
|
439
|
+
ROW = "ROW"
|
|
440
|
+
|
|
441
|
+
@classmethod
|
|
442
|
+
def make_separator(cls, value):
|
|
443
|
+
"""Create a separator token"""
|
|
444
|
+
assert value in (',', ';')
|
|
445
|
+
subtype = cls.ARG if value == ',' else cls.ROW
|
|
446
|
+
return cls(value, cls.SEP, subtype)
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module contains code to translate formulae across cells in a worksheet.
|
|
3
|
+
|
|
4
|
+
The idea is that if A1 has formula "=B1+C1", then translating it to cell A2
|
|
5
|
+
results in formula "=B2+C2". The algorithm relies on the formula tokenizer
|
|
6
|
+
to identify the parts of the formula that need to change.
|
|
7
|
+
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import re
|
|
11
|
+
from .tokenizer import Tokenizer, Token
|
|
12
|
+
from openpyxl.utils import (
|
|
13
|
+
coordinate_to_tuple,
|
|
14
|
+
column_index_from_string,
|
|
15
|
+
get_column_letter
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
class TranslatorError(Exception):
|
|
19
|
+
"""
|
|
20
|
+
Raised when a formula can't be translated across cells.
|
|
21
|
+
|
|
22
|
+
This error arises when a formula's references would be translated outside
|
|
23
|
+
the worksheet's bounds on the top or left. Excel represents these
|
|
24
|
+
situations with a #REF! literal error. E.g., if the formula at B2 is
|
|
25
|
+
'=A1', attempting to translate the formula to B1 raises TranslatorError,
|
|
26
|
+
since there's no cell above A1. Similarly, translating the same formula
|
|
27
|
+
from B2 to A2 raises TranslatorError, since there's no cell to the left of
|
|
28
|
+
A1.
|
|
29
|
+
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Translator:
|
|
34
|
+
|
|
35
|
+
"""
|
|
36
|
+
Modifies a formula so that it can be translated from one cell to another.
|
|
37
|
+
|
|
38
|
+
`formula`: The str string to translate. Must include the leading '='
|
|
39
|
+
character.
|
|
40
|
+
`origin`: The cell address (in A1 notation) where this formula was
|
|
41
|
+
defined (excluding the worksheet name).
|
|
42
|
+
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
def __init__(self, formula, origin):
|
|
46
|
+
# Excel errors out when a workbook has formulae in R1C1 notation,
|
|
47
|
+
# regardless of the calcPr:refMode setting, so I'm assuming the
|
|
48
|
+
# formulae stored in the workbook must be in A1 notation.
|
|
49
|
+
self.row, self.col = coordinate_to_tuple(origin)
|
|
50
|
+
self.tokenizer = Tokenizer(formula)
|
|
51
|
+
|
|
52
|
+
def get_tokens(self):
|
|
53
|
+
"Returns a list with the tokens comprising the formula."
|
|
54
|
+
return self.tokenizer.items
|
|
55
|
+
|
|
56
|
+
ROW_RANGE_RE = re.compile(r"(\$?[1-9][0-9]{0,6}):(\$?[1-9][0-9]{0,6})$")
|
|
57
|
+
COL_RANGE_RE = re.compile(r"(\$?[A-Za-z]{1,3}):(\$?[A-Za-z]{1,3})$")
|
|
58
|
+
CELL_REF_RE = re.compile(r"(\$?[A-Za-z]{1,3})(\$?[1-9][0-9]{0,6})$")
|
|
59
|
+
|
|
60
|
+
@staticmethod
|
|
61
|
+
def translate_row(row_str, rdelta):
|
|
62
|
+
"""
|
|
63
|
+
Translate a range row-snippet by the given number of rows.
|
|
64
|
+
"""
|
|
65
|
+
if row_str.startswith('$'):
|
|
66
|
+
return row_str
|
|
67
|
+
else:
|
|
68
|
+
new_row = int(row_str) + rdelta
|
|
69
|
+
if new_row <= 0:
|
|
70
|
+
raise TranslatorError("Formula out of range")
|
|
71
|
+
return str(new_row)
|
|
72
|
+
|
|
73
|
+
@staticmethod
|
|
74
|
+
def translate_col(col_str, cdelta):
|
|
75
|
+
"""
|
|
76
|
+
Translate a range col-snippet by the given number of columns
|
|
77
|
+
"""
|
|
78
|
+
if col_str.startswith('$'):
|
|
79
|
+
return col_str
|
|
80
|
+
else:
|
|
81
|
+
try:
|
|
82
|
+
return get_column_letter(
|
|
83
|
+
column_index_from_string(col_str) + cdelta)
|
|
84
|
+
except ValueError:
|
|
85
|
+
raise TranslatorError("Formula out of range")
|
|
86
|
+
|
|
87
|
+
@staticmethod
|
|
88
|
+
def strip_ws_name(range_str):
|
|
89
|
+
"Splits out the worksheet reference, if any, from a range reference."
|
|
90
|
+
# This code assumes that named ranges cannot contain any exclamation
|
|
91
|
+
# marks. Excel refuses to create these (even using VBA), and
|
|
92
|
+
# complains of a corrupt workbook when there are names with
|
|
93
|
+
# exclamation marks. The ECMA spec only states that named ranges will
|
|
94
|
+
# be of `ST_Xstring` type, which in theory allows '!' (char code
|
|
95
|
+
# 0x21) per http://www.w3.org/TR/xml/#charsets
|
|
96
|
+
if '!' in range_str:
|
|
97
|
+
sheet, range_str = range_str.rsplit('!', 1)
|
|
98
|
+
return sheet + "!", range_str
|
|
99
|
+
return "", range_str
|
|
100
|
+
|
|
101
|
+
@classmethod
|
|
102
|
+
def translate_range(cls, range_str, rdelta, cdelta):
|
|
103
|
+
"""
|
|
104
|
+
Translate an A1-style range reference to the destination cell.
|
|
105
|
+
|
|
106
|
+
`rdelta`: the row offset to add to the range
|
|
107
|
+
`cdelta`: the column offset to add to the range
|
|
108
|
+
`range_str`: an A1-style reference to a range. Potentially includes
|
|
109
|
+
the worksheet reference. Could also be a named range.
|
|
110
|
+
|
|
111
|
+
"""
|
|
112
|
+
ws_part, range_str = cls.strip_ws_name(range_str)
|
|
113
|
+
match = cls.ROW_RANGE_RE.match(range_str) # e.g. `3:4`
|
|
114
|
+
if match is not None:
|
|
115
|
+
return (ws_part + cls.translate_row(match.group(1), rdelta) + ":"
|
|
116
|
+
+ cls.translate_row(match.group(2), rdelta))
|
|
117
|
+
match = cls.COL_RANGE_RE.match(range_str) # e.g. `A:BC`
|
|
118
|
+
if match is not None:
|
|
119
|
+
return (ws_part + cls.translate_col(match.group(1), cdelta) + ':'
|
|
120
|
+
+ cls.translate_col(match.group(2), cdelta))
|
|
121
|
+
if ':' in range_str: # e.g. `A1:B5`
|
|
122
|
+
# The check is necessarily general because range references can
|
|
123
|
+
# have one or both endpoints specified by named ranges. I.e.,
|
|
124
|
+
# `named_range:C2`, `C2:named_range`, and `name1:name2` are all
|
|
125
|
+
# valid references. Further, Excel allows chaining multiple
|
|
126
|
+
# colons together (with unclear meaning)
|
|
127
|
+
return ws_part + ":".join(
|
|
128
|
+
cls.translate_range(piece, rdelta, cdelta)
|
|
129
|
+
for piece in range_str.split(':'))
|
|
130
|
+
match = cls.CELL_REF_RE.match(range_str)
|
|
131
|
+
if match is None: # Must be a named range
|
|
132
|
+
return range_str
|
|
133
|
+
return (ws_part + cls.translate_col(match.group(1), cdelta)
|
|
134
|
+
+ cls.translate_row(match.group(2), rdelta))
|
|
135
|
+
|
|
136
|
+
def translate_formula(self, dest=None, row_delta=0, col_delta=0):
|
|
137
|
+
"""
|
|
138
|
+
Convert the formula into A1 notation, or as row and column coordinates
|
|
139
|
+
|
|
140
|
+
The formula is converted into A1 assuming it is assigned to the cell
|
|
141
|
+
whose address is `dest` (no worksheet name).
|
|
142
|
+
|
|
143
|
+
"""
|
|
144
|
+
tokens = self.get_tokens()
|
|
145
|
+
if not tokens:
|
|
146
|
+
return ""
|
|
147
|
+
elif tokens[0].type == Token.LITERAL:
|
|
148
|
+
return tokens[0].value
|
|
149
|
+
out = ['=']
|
|
150
|
+
# per the spec:
|
|
151
|
+
# A compliant producer or consumer considers a defined name in the
|
|
152
|
+
# range A1-XFD1048576 to be an error. All other names outside this
|
|
153
|
+
# range can be defined as names and overrides a cell reference if an
|
|
154
|
+
# ambiguity exists. (I.18.2.5)
|
|
155
|
+
if dest:
|
|
156
|
+
row, col = coordinate_to_tuple(dest)
|
|
157
|
+
row_delta = row - self.row
|
|
158
|
+
col_delta = col - self.col
|
|
159
|
+
for token in tokens:
|
|
160
|
+
if (token.type == Token.OPERAND
|
|
161
|
+
and token.subtype == Token.RANGE):
|
|
162
|
+
out.append(self.translate_range(token.value, row_delta,
|
|
163
|
+
col_delta))
|
|
164
|
+
else:
|
|
165
|
+
out.append(token.value)
|
|
166
|
+
return "".join(out)
|