Created
March 7, 2022 03:20
-
-
Save doremi31618/f4d0bd9b4bdf1d0e071888c2c219595f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = class Workbook{ | |
constructor(){ | |
if (this instanceof Workbook) return new Workbook(); | |
this.SheetNames = []; | |
this.Sheets = {}; | |
//define workbook optional object | |
this.wopt = { | |
bookType = 'xlsx', | |
bookSST: false, | |
type: 'binary' | |
}; | |
} | |
appendSheet(sheet, name){ | |
this.sheetNames.push(name); | |
this.Sheets[name] = sheet; | |
} | |
toBlob(option = this.wopts) { | |
// string to ArrayBuffer | |
function s2ab(s) { | |
var buf = new ArrayBuffer(s.length); | |
var view = new Uint8Array(buf); | |
for (var i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; | |
return buf; | |
} | |
var wbout = XLSX.write(this, option); | |
var blob = new Blob([s2ab(wbout)], { type: 'application/octet-stream' }); | |
return blob; | |
} | |
isEmpty() { | |
return !this.SheetNames.length && JSON.stringify(this.Sheets === "{}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment