Sandbox executor
When a task requires Python or Shell scripts (such as installing third-party libraries, calling external APIs, web scraping), use sandboxExec() to execute in a secure sandbox.
Core functions
var result = sandboxExec(language, code, options);
| Parameter | Type | Description |
|---|---|---|
| language | string | "python" or "shell" |
| code | string | The code to execute |
| options.pip | string[] | List of Python dependencies (optional) |
| options.timeout | number | Timeout in seconds, default 60, maximum 120 |
Return value
{
"ok": true,
"stdout": "脚本输出内容",
"stderr": "错误信息(如有)",
"exitCode": 0,
"timedOut": false
}
Usage patterns
Core principle:sandboxExec fetch data → write to the spreadsheet with WPS JavaScript
// 第一步:在沙盒中用 Python 获取数据
var result = sandboxExec("python", `
import json
from google_play_scraper import reviews, Sort
result, _ = reviews(
'com.example.app',
lang='zh',
country='cn',
sort=Sort.NEWEST,
count=50
)
print(json.dumps(result, ensure_ascii=False, default=str))
`, { pip: ["google-play-scraper"], timeout: 90 });
if (!result.ok) return "Python 执行失败: " + result.error;
// 第二步:解析数据并写入表格
var data = JSON.parse(result.stdout);
var ws = Application.ActiveSheet;
ws.Range("A1").Value2 = "用户名";
ws.Range("B1").Value2 = "评分";
ws.Range("C1").Value2 = "评论";
ws.Range("D1").Value2 = "日期";
for (var i = 0; i < data.length; i++) {
var row = i + 2;
ws.Range("A" + row).Value2 = data[i].userName || "";
ws.Range("B" + row).Value2 = data[i].score || 0;
ws.Range("C" + row).Value2 = (data[i].content || "").substring(0, 200);
ws.Range("D" + row).Value2 = data[i].at || "";
}
return "已写入 " + data.length + " 条评论数据";
Common scenarios
Web data scraping
var result = sandboxExec("python", `
import json, urllib.request
url = "https://api.example.com/data"
resp = urllib.request.urlopen(url)
data = json.loads(resp.read())
print(json.dumps(data))
`, { timeout: 30 });
Using third-party Python libraries
var result = sandboxExec("python", `
import json, pandas as pd
df = pd.read_csv("https://example.com/data.csv")
print(df.to_json(orient="records", force_ascii=False))
`, { pip: ["pandas"], timeout: 60 });
Shell commands
var result = sandboxExec("shell", `
curl -s "https://api.exchangerate-api.com/v4/latest/USD" | python3 -c "
import sys, json
data = json.load(sys.stdin)
print(json.dumps(data['rates']))
"
`, { timeout: 30 });
Key rules
- Always complete in a single JavaScript code block:
sandboxExec()fetch data + write to the spreadsheet with the WPS API - Python output must be JSON: at the end of the Python script use
print(json.dumps(...))to output structured data - Check
result.ok: sandbox execution may fail, errors must be handled - pip dependencies are declared in options: do not write
os.system("pip install ...") - Timeout protection: default 60 seconds, set 90-120 seconds for large-data-volume tasks
- Do not operate on the spreadsheet in the sandbox: the sandbox is an isolated environment and cannot access the WPS API. Data must be passed back via JSON
Not applicable scenarios
- Simple WPS spreadsheet operations (use JavaScript directly)
- Data sources that already have a Data Bridge connector (use
dataBridgePull) - Services that require persistent state (the sandbox is disposable)