关于数据累加,无法实现拖动行的位置,根据视觉行数累加数据的脚本方法。
function main() {
console.log("===== [AirScript 2.0] 开始刷新编号 =====");
// 1. 优先从自动化流程入参中获取表名
let sheetName = "";
if (typeof Context !== 'undefined' && Context.argv && Context.argv.sheet_name) {
sheetName = Context.argv.sheet_name;
console.log("📌 读取入参表名:" + sheetName);
} else {
// 兼容脚本编辑器内直接调试:未传参时回退到当前激活表
console.log("⚠️ 未接收到入参,回退使用 ActiveSheet");
if (Application.ActiveSheet) {
sheetName = Application.ActiveSheet.Name;
}
}
if (!sheetName) {
console.log("❌ 获取表名失败,脚本结束");
return;
}
// 2. 获取指定数据表
let sheet;
try {
sheet = Application.Sheets(sheetName);
if (!sheet) {
console.log("❌ 找不到数据表:" + sheetName);
return;
}
console.log("✅ 成功锁定数据表:" + sheet.Name);
} catch (e) {
console.log("❌ 获取数据表异常:" + e.message);
return;
}
// 3. 通过视图对象获取记录区域
let view = sheet.Views(1);
let rg = view.RecordRange;
let rowCount = rg.Count;
console.log("当前视图总行数:" + rowCount);
if (!rowCount || rowCount <= 0) {
console.log("表格中暂无数据,结束执行");
return;
}
const targetField = "@核算累加计数";
let successCount = 0;
// 4. 按当前视图从上到下的顺序逐行赋值
for (let i = 1; i <= rowCount; i++) {
try {
rg(i, targetField).Value = i;
successCount++;
} catch (e) {
console.log(`❌ 第 ${i} 行写入失败:` + e.message);
}
}
console.log(`✅ [2.0] 刷新完成:共 ${rowCount} 行,成功写入 ${successCount} 行`);
}
main();
写一个这样的脚本,然后绑定到按钮,根据传参,辅助累加字段就会根据视图上的顺序重新从1开始排列。
还能如何优化,给点建议。