一个专门用于兼容多维表和智能表的工厂方法,可复用、可扩展!!

/**

* 名称: 用工厂方法模拟兼容多维表和智能表的类(因脚本不支持类)

* 描述: 根据文件类型对数据表的操作进行统一规范

* 作者: CodeSkywalker

* 开发日期: 2024.06.13

* @param {string} jsonObj - {sheetname:'表名'}或{sheetid: 表id},两个都有默认用sheetname

* @return {Object} - 超级表实例

*/

function createSuperApp(jsonObj) {

// 首先检查传入参数

if (typeof jsonObj === 'object' && jsonObj !== null) {

if (!(jsonObj.hasOwnProperty('sheetname') || jsonObj.hasOwnProperty('sheetid'))) {

throw new Error("参数错误,必须给定表名或表id,格式为{sheetname:'表名'}或{sheetid: 表id}。");

}

} else {

throw new Error("参数错误,必须给定表名或表id,格式为{sheetname:'表名'}或{sheetid: 表id}。");

}

const sheetKey = jsonObj.hasOwnProperty('sheetname') ? 'sheetname' : 'sheetid';

console.log(`提取参数:${sheetKey} = ${jsonObj[sheetKey]}`);

let superapp = {};

// 根据 doctype 初始化 app 对象

superapp.doctype = Application.FileInfo.officeType;

if (sheetKey === "sheetname") {

superapp.name = jsonObj[sheetKey]

superapp.app = superapp.doctype === "k" ? Application.Sheets(jsonObj[sheetKey]) : Application;

superapp.sheetobj = superapp.doctype === "k" ?

Application.Sheets(jsonObj[sheetKey]) :

Array.from(Application.Sheet.GetSheets()).find(sheet => sheet.name === jsonObj[sheetKey]);

if (superapp.sheetobj === null || superapp.sheetobj === undefined) {

throw new Error(`错误的表名: 未找到"${jsonObj[sheetKey]}"。`);

}

superapp.id = superapp.doctype === "k" ? superapp.sheetobj.Id : superapp.sheetobj.id;

}

else {

superapp.id = jsonObj[sheetKey];

superapp.sheetobj = superapp.doctype === "k" ?

Array.from(Application.Sheets).find(sheet => sheet.Id === jsonObj[sheetKey]) :

Array.from(Application.Sheet.GetSheets()).find(sheet => sheet.id === jsonObj[sheetKey]);

if (superapp.sheetobj === null || superapp.sheetobj === undefined) {

throw new Error(`错误的表名: 未找到"${jsonObj[sheetKey]}"。`);

}

superapp.name = superapp.doctype === "k" ? superapp.sheetobj.Name : superapp.sheetobj.name;

superapp.app = superapp.doctype === "k" ? Application.Sheets(superapp.name) : Application;

}

// 定义 GetRecords 方法

superapp.GetRecords = function (jsonObj) {

console.log(jsonObj)

if (jsonObj === null || jsonObj === undefined) {

return this.app.Record.GetRecords({ "SheetId": this.id });

}

else if (typeof jsonObj === 'object') {

jsonObj["SheetId"] = this.id;

console.log("superapp.GetRecords传入参数:", jsonObj)

return this.app.Record.GetRecords(jsonObj);

}

else {

throw new Error('错误参数: 必须是一个json对象或者空值。');

}

};

//定义 GetRecord 方法

superapp.GetRecord = function (jsonObj) {

console.log(typeof jsonObj)

if (typeof jsonObj === 'object') {

jsonObj["SheetId"] = this.id;

console.log("superapp.GetRecord传入参数:", jsonObj)

return this.app.Record.GetRecord(jsonObj);

}

else {

throw new Error('错误参数: 必须是一个json对象。');

}

};

//定义 CreateRecords 方法

superapp.CreateRecords = function (jsonObj) {

console.log(typeof jsonObj)

if (typeof jsonObj === 'object') {

jsonObj["SheetId"] = this.id;

console.log("superapp.CreateRecords传入参数:", jsonObj)

return this.app.Record.CreateRecords(jsonObj);

}

else {

throw new Error('错误参数: 必须是一个json对象。');

}

};

// 返回初始化后的 app 对象

return superapp;

}

详细请查看

广东省
浏览 280
1
4
分享
4 +1
4
1 +1
全部评论 4
 
CodeSky
不需要看懂,只需要知道怎么用。比如,你想读取“商品表”的记录: 1.把上述代码复制到自己的代码中; 2.创建兼容结构体:const mysheet = createSuperApp({sheetname:"商品表"}); 3.像智能表脚本一样执行getrecords,比如:const myrecords = mysheet.GetRecords(); 看起来与多维表的脚本差不多是不是?那我为什么要写这个呢?因为有的时候你用多维表的时候,发现多维表不好用,想另存为智能表,这样你的脚本全部要重写,因为智能表与多维表的api不一样!!!而我这个函数可以根据文件类型调用不同的api,从而实现兼容多维表和智能表。有这个需要的可以试试。
· 广东省
回复
 
lvelvelve
(完全看不懂) 但感觉好厉害
· 江苏省
回复
 
ㅤㅤㅤㅤㅤ
(完全看不懂) 但感觉好厉害
· 陕西省
回复
 
清华学弟任泽岩
清华学弟任泽岩

创作者俱乐部成员

(完全看不懂) 但感觉好厉害
· 辽宁省
回复