自定义函数 JSA_A_STRINGTOBASE64STRING 字符串转base64字符串

function JSA_A_STRINGTOBASE64STRING(string, label, endianness, hasBom) { // String 转 base64String
       //表格中使用函数,参数留空时,默认值处理
    string = (string === undefined || string === -2147352572) ? undefined : string;
    label = (label === undefined || label === -2147352572) ? undefined : label;
    endianness = (endianness === undefined || endianness === -2147352572) ? undefined : endianness;
    hasBom = (hasBom === undefined || hasBom === -2147352572) ? undefined : hasBom;
    
    // 参数验证和默认值处理
    string = Array.isArray(string.valueOf()) ? string.valueOf().flat(Infinity).join("") : string.valueOf();
    label = (label || 'utf-8').toLowerCase();
    endianness = (endianness || 'BE').toUpperCase();
    hasBom = hasBom || false;
    
    // 验证参数有效性
    if (string === undefined || string === "") {
        return "#N/A";
    }
    
    if (!['utf-8', 'utf-16'].includes(label)) {
        return "#N/A";
    }
    
    if (!['BE', 'LE'].includes(endianness)) {
        return "#N/A";
    }
    
    if (label === 'utf-8') {
        let uint8Array = Run("JSA_A_STRINGTOUTF8TOUINT8ARRAY", string, undefined);
        let base64 = Run("JSA_A_UINT8ARRAYTOBASE64STRING", uint8Array);
        return base64;
    }
    
    if (label == 'utf-16') {
        let uint8Array = Run("JSA_A_STRINGTOUTF16TOUINT8ARRAY", string, undefined, endianness, hasBom);
        let base64 = Run("JSA_A_UINT8ARRAYTOBASE64STRING", uint8Array);
        return base64;
    }
}

插入函数对话框、函数参数对话框:

加载宏文件function Workbook_Open(){}中添加以下代码。

Application.MacroOptions("JSA_A_STRINGTOBASE64STRING", "字符串 转 base64字符串", undefined, undefined, undefined, undefined, 14, undefined, undefined, undefined, ['字符串。', '字符串编码。"utf-8"(默认)、"utf-16"。', '字节序,"utf-16"需指定。"BE":大端序(高位字节在前)(默认),"LE":小端序(低位字节在前)。', '是否添加字节序标记(BOM),"utf-16"需指定。TRUE:添加,FALSE:不添加(默认)。']);

云南省
浏览 13
收藏
1
分享
1 +1
1
+1
全部评论 1
 
497128657
关系图:https://bbs.wps.cn/topic/68275
· 云南省
回复