smartsheet-tools 0.0.3__tar.gz → 0.0.6__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: smartsheet_tools
3
- Version: 0.0.3
3
+ Version: 0.0.6
4
4
  Summary: A collection of convenience functions to aid with transitioning from simple-smartsheet to the SDK API and common tasks
5
5
  Author: Ashton Pooley
6
6
  Author-email: Ashton Pooley <ashton@ashi.digital>
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "smartsheet_tools" # from setup.py
7
- version = "0.0.3" # from setup.py
7
+ version = "0.0.6" # from setup.py
8
8
  description = "A collection of convenience functions to aid with transitioning from simple-smartsheet to the SDK API and common tasks"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9" # matches classifiers (3.9–3.12)
@@ -2,7 +2,7 @@ from setuptools import setup
2
2
 
3
3
  setup(
4
4
  name="smartsheet_tools",
5
- version="0.0.3",
5
+ version="0.0.6",
6
6
  description="A collection of convenience functions to aid with transitioning from simple-smartsheet to the SDK API and common tasks",
7
7
  author="Ashton Pooley",
8
8
  author_email="Ashton@ashi.digital",
@@ -1,9 +1,12 @@
1
1
  from datetime import datetime
2
2
  import re
3
3
  from smartsheet.models import Cell, Row, Folder, Sheet
4
+ from smartsheet.models import Column
4
5
 
5
6
  # Cache for column types to minimize API calls when correcting date formats
6
7
  _COLUMN_TYPE_CACHE = {}
8
+ _TITLE_TO_ID_CACHE = {}
9
+ _ID_TO_INDEX_CACHE = {}
7
10
 
8
11
  def norm(v):
9
12
  if v is None:
@@ -18,11 +21,15 @@ def disp_or_val(cell):
18
21
 
19
22
  def title_to_index(sheet):
20
23
  # authoritative positions from Smartsheet (not Python enumerate order)
21
- return {c.title: c.index for c in sheet.columns}
24
+ if sheet.id not in _TITLE_TO_ID_CACHE:
25
+ _TITLE_TO_ID_CACHE[sheet.id] = {c.title: c.index for c in sheet.columns}
26
+ return _TITLE_TO_ID_CACHE[sheet.id]
22
27
 
23
28
  def index_to_id(sheet):
24
29
  # authoritative positions from Smartsheet (not Python enumerate order)
25
- return {c.index: c.id for c in sheet.columns}
30
+ if sheet.id not in _ID_TO_INDEX_CACHE:
31
+ _ID_TO_INDEX_CACHE[sheet.id] = {c.index: c.id for c in sheet.columns}
32
+ return _ID_TO_INDEX_CACHE[sheet.id]
26
33
 
27
34
  def id_to_index(sheet):
28
35
  # authoritative positions from Smartsheet (not Python enumerate order)
@@ -48,17 +55,20 @@ def standard_time_to_isoformat(st):
48
55
  return None
49
56
  return datetime_to_isoformat(datetime.strptime(st, "%m/%d/%Y"))
50
57
 
51
- def get_cached_column_type(column_id, sheet_obj):
58
+ def get_cached_column_type(column_id, sheet_obj, prefill=False):
52
59
  if sheet_obj.id not in _COLUMN_TYPE_CACHE:
53
60
  _COLUMN_TYPE_CACHE[sheet_obj.id] = {}
54
61
 
55
62
  if column_id not in _COLUMN_TYPE_CACHE[sheet_obj.id]:
56
- _COLUMN_TYPE_CACHE[sheet_obj.id][column_id] = str(sheet_obj.get_column(column_id).type)
63
+ if not prefill:
64
+ _COLUMN_TYPE_CACHE[sheet_obj.id][column_id] = str(sheet_obj.get_column(column_id).type)
65
+ else:
66
+ _COLUMN_TYPE_CACHE[sheet_obj.id][column_id] = prefill
57
67
 
58
68
  return _COLUMN_TYPE_CACHE[sheet_obj.id][column_id]
59
69
 
60
70
  def get_col_names_of_date_cols(sheet_obj):
61
- return [c.title for c in sheet_obj.columns if get_cached_column_type(c.id, sheet_obj) in ("DATE", "DATETIME")]
71
+ return [c.title for c in sheet_obj.columns if get_cached_column_type(c.id, sheet_obj, prefill=c.type) in ("DATE", "DATETIME")]
62
72
 
63
73
  def brute_force_date_string(s, nonetype_if_fail=False):
64
74
  # attempt to parse a date string in common formats to ISO 8601
@@ -92,18 +102,18 @@ def correct_date_format(isoformat_datetime, column_id, sheet_obj):
92
102
  return isoformat_datetime
93
103
  return None
94
104
 
95
- def new_cell(column_id, value=None, strict=False, formula=None):
105
+ def new_cell(column_id=None, value=None, strict=False, formula=None):
96
106
  new_cell = Cell()
97
- new_cell.column_id = column_id
107
+ if column_id is not None:
108
+ new_cell.column_id = column_id
98
109
  if formula is not None:
99
110
  new_cell.formula = formula
100
111
  else:
101
112
  new_cell.value = value
102
- if strict:
103
- new_cell.strict = True
113
+ new_cell.strict = strict
104
114
  return new_cell
105
115
 
106
- def new_row(cells=None, id=None, parent_id=None, to_top=False):
116
+ def new_row(cells=None, id=None, parent_id=None, to_top=False, locked=False):
107
117
  new_row = Row()
108
118
  if cells:
109
119
  new_row.cells = cells
@@ -113,6 +123,8 @@ def new_row(cells=None, id=None, parent_id=None, to_top=False):
113
123
  new_row.parent_id = parent_id
114
124
  if to_top:
115
125
  new_row.to_top = to_top
126
+ if locked:
127
+ new_row.locked = locked
116
128
  return new_row
117
129
 
118
130
  def walk_folder_for_sheets(smartsheet_client, folder_id):
@@ -143,4 +155,25 @@ def walk_workspace_for_folders(smartsheet_client, workspace_id):
143
155
 
144
156
  def walk_sheet_names_from_workspace(smartsheet_client, workspace_id):
145
157
  for sheet in walk_workspace_for_sheets(smartsheet_client, workspace_id):
146
- yield sheet.name
158
+ yield sheet.name
159
+
160
+ def new_column(column_type, title, index=None, id=None, options=None, symbol=None, primary=False, hidden=False, locked=False):
161
+ new_column = Column()
162
+
163
+ new_column.type = column_type
164
+ new_column.title = title
165
+ if index is not None:
166
+ new_column.index = index
167
+ if id is not None:
168
+ new_column.id = id
169
+ if options is not None and column_type in ("PICKLIST", "MULTI_PICKLIST"):
170
+ new_column.options = options
171
+ if symbol is not None and column_type == "CHECKBOX":
172
+ new_column.symbol = symbol
173
+ if primary:
174
+ new_column.primary = True
175
+ if hidden:
176
+ new_column.hidden = True
177
+ if locked:
178
+ new_column.locked = True
179
+ return new_column
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: smartsheet_tools
3
- Version: 0.0.3
3
+ Version: 0.0.6
4
4
  Summary: A collection of convenience functions to aid with transitioning from simple-smartsheet to the SDK API and common tasks
5
5
  Author: Ashton Pooley
6
6
  Author-email: Ashton Pooley <ashton@ashi.digital>