Generate Job Numbers¶
Generate Job Numbers
Python script that automatically assigns sequential job numbers to Smartsheet rows, with duplicate prevention and configurable starting values.
Python 100% Active
Overview¶
Automates the assignment of unique, sequential job numbers to Smartsheet rows. Eliminates manual numbering errors and ensures consistency across your job tracking system.
GitHub Repository: JFlo21/generate-job-numbers
Key Features¶
- **Auto-Increment**: Automatic sequential numbering - **Duplicate Prevention**: Ensures unique job numbers - **Configurable Start**: Set custom starting number - **Gap Handling**: Fills gaps in number sequence - **Batch Processing**: Efficient handling of multiple rows - **Dry-Run Mode**: Test before assigning - **Webhook Ready**: Can be triggered by Smartsheet webhooks
Use Cases¶
- Assigning job numbers to new projects
- Creating unique identifiers for work orders
- Generating sequential purchase order numbers
- Auto-numbering service requests
Architecture¶
graph LR
SS[Smartsheet] -->|Query Rows<br/>Without Job #| PY[Python Script]
PY -->|Find Last<br/>Number| SS
PY -->|Calculate<br/>Next| PY
PY -->|Update Rows| SS
style SS fill:#f9f,stroke:#333,stroke-width:2px
style PY fill:#3776ab,stroke:#333,stroke-width:2px File Structure¶
generate-job-numbers/
├── generate_job_numbers.py # Main script
├── config.py # Configuration
├── requirements.txt # Dependencies
├── .env.example # Environment template
└── README.md # Documentation
Environment Variables¶
| Variable | Required | Description | Example |
|---|---|---|---|
SMARTSHEET_ACCESS_TOKEN | Yes | API token | ll... |
SHEET_ID | Yes | Target sheet ID | 1234567890123456 |
COLUMN_ID_JOB_NUMBER | Yes | Job number column ID | 1234567890123456 |
STARTING_NUMBER | No | First job number (default: 1000) | 1000 |
PREFIX | No | Number prefix | JOB- |
DRY_RUN | No | Test mode | false |
Setup Instructions¶
1. Clone Repository¶
2. Install Dependencies¶
3. Configure Environment¶
SMARTSHEET_ACCESS_TOKEN=your_token
SHEET_ID=1234567890123456
COLUMN_ID_JOB_NUMBER=1111111111111111
STARTING_NUMBER=1000
PREFIX=JOB-
DRY_RUN=false
Usage Examples¶
Assign Job Numbers¶
Output:
Found 3 rows without job numbers
Last assigned number: JOB-1047
Assigning JOB-1048 to row 123456789
Assigning JOB-1049 to row 234567890
Assigning JOB-1050 to row 345678901
Successfully assigned 3 job numbers
Dry-Run Mode¶
Fill Gaps¶
Custom Starting Number¶
Configuration¶
Number Format¶
Simple Sequential:
With Prefix:
With Padding:
Filtering Rows¶
Only assign numbers to rows meeting criteria:
def should_assign_number(row):
# Only assign if status is "Approved"
status_cell = get_cell_by_column_id(row, COLUMN_ID_STATUS)
return status_cell.value == "Approved"
Dependencies¶
Webhook Integration¶
Setup Webhook¶
- In Smartsheet, go to Sheet → Webhooks
- Create new webhook pointing to your server
- Enable on "Row Added" events
Webhook Handler¶
from flask import Flask, request
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def handle_webhook():
data = request.json
events = data.get('events', [])
# Check for new rows
if any(e['objectType'] == 'row' for e in events):
os.system('python generate_job_numbers.py')
return '', 200
if __name__ == '__main__':
app.run(port=5000)
Error Handling¶
Duplicate Detection¶
def check_duplicates(sheet_id, column_id):
sheet = client.Sheets.get_sheet(sheet_id)
numbers = []
for row in sheet.rows:
cell = get_cell_by_column_id(row, column_id)
if cell.value:
numbers.append(cell.value)
duplicates = [n for n in numbers if numbers.count(n) > 1]
if duplicates:
raise ValueError(f"Duplicate job numbers found: {set(duplicates)}")
Conflict Resolution¶
def assign_with_retry(row_id, job_number, max_retries=3):
for attempt in range(max_retries):
try:
update_row(row_id, job_number)
return
except ConflictError:
# Number was assigned concurrently, get next
job_number = get_next_available_number()
except Exception as e:
if attempt < max_retries - 1:
time.sleep(1)
else:
raise
Monitoring¶
Track Assignments¶
# Log all assignments
def log_assignment(row_id, job_number):
with open('assignments.log', 'a') as f:
f.write(f"{datetime.now().isoformat()},{row_id},{job_number}\n")
Metrics¶
- Total numbers assigned
- Assignment duration
- Duplicate attempts
- Gap fills
Troubleshooting¶
Numbers Not Assigning¶
- Check column ID is correct
- Verify API token has write access
- Ensure target column is not a formula
Duplicate Numbers¶
# Check for duplicates
python -c "from generate_job_numbers import check_duplicates; check_duplicates('SHEET_ID', COLUMN_ID)"
Wrong Starting Number¶
Verify last assigned number:
Related Repositories¶
- Master to Sibling - Sheet replication
- Smartsheet Supabase Sync - Database sync