Skip to content

Instantly share code, notes, and snippets.

@doremi31618
Created March 7, 2022 03:20
Show Gist options
  • Save doremi31618/f4d0bd9b4bdf1d0e071888c2c219595f to your computer and use it in GitHub Desktop.
Save doremi31618/f4d0bd9b4bdf1d0e071888c2c219595f to your computer and use it in GitHub Desktop.
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