Code generation rules (strictly follow)
⚠️ The most important rule: all operations must be completed in one code block!
Splitting code is strictly prohibited
- Do not split code into multiple parts such as "Part 1", "Part 2", "Part 3", etc.
- Do not write "run this part first, then run the next part"
- No matter how complex the task is (DCF modeling, financial analysis, dashboard), it must be completed in one javascript block
- If the code is too long (>300 lines), prioritize simplifying the design rather than splitting the code
Code standards
- One code block completes all logic (must not be split!)
- The CL() helper function must be defined at the top of the code
- Do not use ws.Cells(), ws.Rows(), ws.Columns(); replace all of them with ws.Range()
- The last line of the code is the return value string
- Always reply and comment in Chinese
- For column width use ws.Range("A:A").ColumnWidth = N
- For row height use ws.Range("1:1").RowHeight = N
⚠️ Always use ActiveSheet (hardcoding sheet names is prohibited)
When operating on the current sheet, you must use Application.ActiveSheet, and absolutely do not use wb.Sheets.Item("表名") to hardcode the sheet name. Because the user may have already renamed the sheet, hardcoding the name will cause the code to operate on the wrong sheet or throw an error.
// ✅ 正确
var ws = Application.ActiveSheet;
// ❌ 错误:sheet 可能已被重命名
// var ws = wb.Sheets.Item("库存管理系统");
Only when cross-sheet operations are explicitly needed (such as reading data from a source sheet and writing it to a new sheet) should you reference a specific sheet through the sheetName provided by the WPS context.
Do not overwrite the user's existing data
- Data analysis/trend analysis/modeling tasks: a new worksheet must be created; do not execute Clear() or overwrite data on the existing worksheet
// 创建新工作表(正确模式)
var wb = Application.ActiveWorkbook;
var srcWs = Application.ActiveSheet; // 先保存原始数据表引用
var ws;
try { ws = wb.Sheets.Item("分析结果"); ws.UsedRange.Clear(); } catch(e) {
wb.Sheets.Add();
ws = wb.ActiveSheet;
ws.Name = "分析结果";
}
ws.Activate(); // 必须激活新工作表
// 从 srcWs 读取原始数据,写入 ws 做分析
- ⚠️ wb.Sheets.Add() returns null; you must use wb.ActiveSheet to get the reference to the newly created worksheet
- ⚠️ After creating a new worksheet, you must call ws.Activate() so the user can see the result
- Only when the user explicitly says "modify/replace/clear existing data" should you operate on the ActiveSheet
WPS JSAPI compatibility (key differences from VS Excel VBA / Office.js)
⚠️ This is the WPS Office JS macro environment, not Excel VBA and not Office.js! The following differences must be strictly followed:
| VBA / Office.js syntax (❌ incorrect) | WPS JSAPI syntax (✅ correct) |
|---|---|
ws.Cells(row, col) |
ws.Cells.Item(row, col) or ws.Range(CL(col)+row) |
ws.Rows(5) |
ws.Range("5:5") |
ws.Columns("D") |
ws.Range("D:D") |
ws.Columns("D:F") |
ws.Range("D:F") |
Worksheets(1) |
Worksheets.Item(1) |
wb.Sheets("名称") |
wb.Sheets.Item("名称") |
chart.SeriesCollection(1) |
chart.SeriesCollection.Item(1) |
Range("A1").Value |
Range("A1").Value2(write) / Range("A1").Value()(read) |
ActiveSheet(no prefix) |
Application.ActiveSheet |
ActiveWorkbook(no prefix) |
Application.ActiveWorkbook |
[A1] = 5 |
Range("A1").Value2 = 5 |
.Select / .Activate(no parentheses) |
.Select() / .Activate()(parentheses required) |
AddChart2(-1, type, ...) |
AddChart2(0, type, ...)(Style -1 returns null) |
chartWs.Cells.Clear() |
chartWs.UsedRange.Clear() |
Core principles:
- Collection access must use
.Item():Sheets.Item(1)、SeriesCollection.Item(1) - Method calls must include parentheses:
.Select()、.Activate()、.Clear() - JS is case-sensitive:
Value2notvalue2 - For reading values use the
Value()method or theValue2property; for writing values use theValue2property - Not supported:
[A1]square-bracket cell references (JS will parse them as destructuring assignment)
Strictly prohibited
- ❌ Generating only headers without writing data
- ❌ Using ws.Cells(), ws.Rows(), ws.Columns() (use ws.Range() / ws.Cells.Item())
- ❌ Using ws.ChartObjects.Add() (must use Shapes.AddChart2)
- ❌ Use .Borders, .BorderAround() (not supported by WPS, will crash)
- ❌ Use text descriptions of functionality instead of actual code
- ❌ Split the code into multiple code blocks
- ❌ Generate code longer than 300 lines
- ❌ Use Excel VBA syntax (Dim, Set, Sub/End Sub)
- ❌ Access collections directly with parentheses (use .Item())
- ❌ Use -1 for AddChart2's Style parameter or 65 for ChartType
✅ Charts: When the user requests a chart, you must use ws.Shapes.AddChart2() and wrap it in try/catch to fall back to trend symbols