产品一张表luckysheet前端代码库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.1 KiB

5 years ago
import { columeHeader_word, columeHeader_word_index } from '../controllers/constant';
5 years ago
/**
* Common tool methods
*/
/**
* Determine whether a string is in standard JSON format
* @param {String} str
*/
5 years ago
export function isJsonString(str) {
5 years ago
try {
if (typeof JSON.parse(str) == "object") {
return true;
}
5 years ago
}
catch (e) { }
5 years ago
return false;
}
5 years ago
/**
* extend two objects
* @param {Object } jsonbject1
* @param {Object } jsonbject2
*/
5 years ago
export function common_extend(jsonbject1, jsonbject2) {
5 years ago
var resultJsonObject = {};
for (var attr in jsonbject1) {
resultJsonObject[attr] = jsonbject1[attr];
}
for (var attr in jsonbject2) {
resultJsonObject[attr] = jsonbject2[attr];
}
return resultJsonObject;
};
5 years ago
// 替换temp中的${xxx}为指定内容 ,temp:字符串,这里指html代码,dataarry:一个对象{"xxx":"替换的内容"}
// 例:luckysheet.replaceHtml("${image}",{"image":"abc","jskdjslf":"abc"}) ==> abc
export function replaceHtml(temp, dataarry) {
return temp.replace(/\$\{([\w]+)\}/g, function (s1, s2) { var s = dataarry[s2]; if (typeof (s) != "undefined") { return s; } else { return s1; } });
};
//获取数据类型
export function getObjType(obj) {
var toString = Object.prototype.toString;
var map = {
'[object Boolean]': 'boolean',
'[object Number]': 'number',
'[object String]': 'string',
'[object Function]': 'function',
'[object Array]': 'array',
'[object Date]': 'date',
'[object RegExp]': 'regExp',
'[object Undefined]':'undefined',
'[object Null]': 'null',
'[object Object]': 'object'
}
// if(obj instanceof Element){
// return 'element';
// }
return map[toString.call(obj)];
}
//列下标 字母转数字
export function ABCatNum(abc) {
abc = abc.toUpperCase();
let abc_len = abc.length;
if (abc_len == 0) {
return NaN;
}
let abc_array = abc.split("");
let wordlen = columeHeader_word.length;
let ret = 0;
for (var i = abc_len - 1; i >= 0; i--) {
if (i == abc_len - 1) {
ret += columeHeader_word_index[abc_array[i]];
}
else {
ret += Math.pow(wordlen, abc_len - i - 1) * (columeHeader_word_index[abc_array[i]] + 1);
}
}
return ret;
};
//列下标 数字转字母
export function chatatABC(index) {
let wordlen = columeHeader_word.length;
if (index < wordlen) {
return columeHeader_word[index];
}
else {
let last = 0, pre = 0, ret = "";
let i = 1, n = 0;
while (index >= (wordlen / (wordlen - 1)) * (Math.pow(wordlen, i++) - 1)) {
n = i;
}
let index_ab = index - (wordlen / (wordlen - 1)) * (Math.pow(wordlen, n - 1) - 1);//970
last = index_ab + 1;
for (let x = n; x > 0; x--) {
let last1 = last, x1 = x;//-702=268, 3
if (x == 1) {
last1 = last1 % wordlen;
if (last1 == 0) {
last1 = 26;
}
return ret + columeHeader_word[last1 - 1];
}
last1 = Math.ceil(last1 / Math.pow(wordlen, x - 1));
//last1 = last1 % wordlen;
ret += columeHeader_word[last1 - 1];
if (x > 1) {
last = last - (last1 - 1) * wordlen;
}
}
}
};
//计算字符串字节长度
export function getByteLen(val) {
if(val == null){
return 0;
}
let len = 0;
for (let i = 0; i < val.length; i++) {
let a = val.charAt(i);
if (a.match(/[^\x00-\xff]/ig) != null) {
len += 2;
}
else {
len += 1;
}
}
return len;
};
//数组去重
export function ArrayUnique(dataArr) {
let arr = [];
if(dataArr.length > 0){
for(let i = 0; i < dataArr.length; i++){
if(arr.indexOf(dataArr[i]) == -1){
arr.push(dataArr[i]);
}
}
}
return arr;
5 years ago
}