JSONのシリアル化と逆シリアル化
テーブルシートのJSONシリアル化と逆シリアル化は、ワークブックの toJSON と fromJSON メソッドを使用して行われます。
SJS形式のシリアル化と逆シリアル化
テーブルシートのSJS形式のシリアル化と逆シリアル化は、ワークブックの save と openメソッドを使用して行われます。
Excelのエクスポート
テーブルシートのExcelへのエクスポートは、ワークブックの exportメソッドを使用して行われます。
PDFのエクスポート
テーブルシートのPDFへのエクスポートは、ワークブックのsavePDFメソッドを使用して行われます。印刷またはPDFへのエクスポートのために、printInfoメソッドを使用して印刷情報をカスタマイズできます。
印刷
テーブルシートの印刷は、ワークブックのprintメソッドを使用して行われます。
テーブルシートは、columnStart/columnEnd/rowStart/rowEnd、repeatColumnStart/repeatColumnEnd/repeatRowStart/repeatRowEnd、およびshowRowHeaderを除く、印刷情報のほとんどのプロパティをサポートします。
<template>
<div class="sample-tutorial">
<gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread">
</gc-spread-sheets>
<div class="options-container">
<fieldset>
<legend>保存</legend>
<div class="field-line">
<input type="button" id="toJSON" value="JSON" class="button" @click="onToJSON">
</div>
<div class="field-line">
<input type="button" id="toSJS" value="SJS" class="button" @click="onToSJS">
</div>
<div class="field-line">
<input type="button" id="saveExcel" value="Excel" class="button" @click="onSaveExcel">
</div>
</fieldset>
<fieldset>
<legend>読み込み</legend>
<div class="field-line">
<input type="file" id="fileDemo" class="input">
</div>
<div class="field-line">
<input type="button" id="fromJSONOrSJS" value="JSON or SJS" class="button" @click="onFromJSONOrSJS">
</div>
</fieldset>
<fieldset>
<legend>レンダリング</legend>
<div class="field-line">
<input type="button" id="exportPDF" value="PDF" class="button" @click="onExportPDF">
</div>
<div class="field-line">
<input type="button" id="print" value="印刷" class="button" @click="onPrint">
</div>
</fieldset>
</div>
</div>
</template>
<script setup>
import { ref } from "vue";
import GC from "@mescius/spread-sheets";
import '@mescius/spread-sheets-print';
import '@mescius/spread-sheets-pdf';
import "@mescius/spread-sheets-io";
import "@mescius/spread-sheets-tablesheet";
import '@mescius/spread-sheets-resources-ja';
GC.Spread.Common.CultureManager.culture("ja-jp");
import "@mescius/spread-sheets-vue";
const spreadRef = ref(null);
function initSpread(spread) {
spreadRef.value = spread;
spread.suspendPaint();
spread.options.autoFitType = GC.Spread.Sheets.AutoFitType.cellWithHeader;
spread.options.highlightInvalidData = true;
//init a data manager
var baseApiUrl = getBaseApiUrl();
var dataManager = spread.dataManager();
//add product table
var productTable = dataManager.addTable("productTable", {
remote: {
read: {
url: baseApiUrl + "/Product"
}
}
});
//init a table sheet
var sheet = spread.addSheetTab(0, "TableSheet1", GC.Spread.Sheets.SheetType.tableSheet);
sheet.options.allowAddNew = false; //hide new row
sheet.setDefaultRowHeight(40, GC.Spread.Sheets.SheetArea.colHeader);
//bind a view to the table sheet
var numericStyle = {};
numericStyle.formatter = "$ 0.00";
var formulaRule = {
ruleType: "formulaRule",
formula: "@>=50",
style: {
font: "bold 12pt Calibri",
backColor: "#F7D3BA",
foreColor: "#F09478"
}
};
var positiveNumberValidator = {
type: "formula",
formula: '@<50',
inputTitle: 'Data validation:',
inputMessage: 'Enter a number smaller than 50.',
highlightStyle: {
type: 'icon',
color: "#F09478",
position: 'outsideRight',
}
};
var myView = productTable.addView("myView", [
{ value: "Id", caption: "ID", width: 46 },
{ value: "ProductName", caption: "Name", width: 200 },
{ value: "UnitPrice", caption: "Unit Price", width: 140, conditionalFormats: [formulaRule], validator: positiveNumberValidator, style: numericStyle },
{ value: "=SUM([@UnitsInStock] , [@UnitsOnOrder])", caption: "Total Units", width: 140 },
{ value: "Discontinued", width: 120, style: { formatter: "[green]✔;;[red]✘" } }
]);
myView.fetch().then(function () {
sheet.setDataView(myView);
});
spread.resumePaint();
}
function getBaseApiUrl() {
return window.location.href.match(/http.+spreadjs\/demos\//)[0] + 'server/api';
}
function onToJSON() {
var spread = spreadRef.value;
var json = spread.toJSON({
includeBindingSource: true,
saveAsView: true
});
saveAs(new Blob([JSON.stringify(json)], {
type: "text/plain;charset=utf-8"
}), 'download.ssjson');
}
function onToSJS() {
var spread = spreadRef.value;
spread.save(function (blob) {
// save blob to a file
saveAs(blob, 'download.sjs');
}, function (e) {
console.log(e);
});
}
function onFromJSONOrSJS() {
var spread = spreadRef.value;
var file = document.getElementById("fileDemo").files[0];
if (file) {
var suffix = file.name.substr(file.name.lastIndexOf(".") + 1).toLowerCase();
if (suffix === "ssjson" || suffix === "json") {
var reader = new FileReader();
reader.onload = function () {
var json = JSON.parse(this.result);
spread.fromJSON(json);
};
reader.readAsText(file);
} else if (suffix === 'sjs') {
spread.open(file, function () {
// success callback to do something
}, function (e) {
console.log(e); // error callback
});
}
}
}
function onSaveExcel() {
var spread = spreadRef.value;
spread.export(function (blob) {
// save blob to a file
saveAs(blob, "export.xlsx");
}, function (e) {
console.log(e);
}, {
fileType: GC.Spread.Sheets.FileType.excel
});
}
function onExportPDF() {
var spread = spreadRef.value;
spread.savePDF(function (blob) {
saveAs(blob, "export.pdf");
}, function (error) {
console.log(error);
}, null, spread.getSheetCount() + spread.getActiveSheetTabIndex());
}
function onPrint() {
var spread = spreadRef.value;
spread.print();
}
</script>
<style scoped>
#app {
height: 100%;
}
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: calc(100% - 280px);
height: 100%;
overflow: hidden;
float: left;
}
.options-container {
float: right;
width: 280px;
padding: 3px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
}
.sample-options {
z-index: 1000;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
fieldset {
padding: 6px;
margin: 0;
margin-top: 10px;
}
fieldset span,
fieldset input,
fieldset select {
display: inline-block;
text-align: left;
}
fieldset input[type=text] {
width: calc(100% - 58px);
}
fieldset input[type=button] {
width: 100%;
text-align: center;
}
fieldset input[type=file] {
width: 100%;
text-align: left;
}
fieldset select {
width: calc(100% - 50px);
}
.field-line {
margin-top: 4px;
}
.field-inline {
display: inline-block;
vertical-align: middle;
}
fieldset label.field-inline {
width: 100px;
}
fieldset input.field-inline {
width: calc(100% -100px - 12px);
}
</style>
<!DOCTYPE html>
<html lang="en" style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>SpreadJS VUE</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/ja/vue3/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/spread/source/js/FileSaver.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ja/vue3/node_modules/systemjs/dist/system.src.js"></script>
<script src="./systemjs.config.js"></script>
<script src="./compiler.js" type="module"></script>
<script>
var System = SystemJS;
System.import("./src/app.js");
System.import('$DEMOROOT$/ja/lib/vue3/license.js');
</script>
</head>
<body>
<div id="app"></div>
</body>
</html>
(function (global) {
SystemJS.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
packageConfigPaths: [
'./node_modules/*/package.json',
"./node_modules/@mescius/*/package.json",
"./node_modules/@babel/*/package.json",
"./node_modules/@vue/*/package.json"
],
map: {
'vue': "npm:vue/dist/vue.esm-browser.js",
'tiny-emitter': 'npm:tiny-emitter/index.js',
'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js',
"systemjs-babel-build": "npm:systemjs-plugin-babel/systemjs-babel-browser.js",
'@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js',
'@mescius/spread-sheets-resources-ja': 'npm:@mescius/spread-sheets-resources-ja/index.js',
'@mescius/spread-sheets-io': 'npm:@mescius/spread-sheets-io/index.js',
'@mescius/spread-sheets-print': 'npm:@mescius/spread-sheets-print/index.js',
'@mescius/spread-sheets-pdf': 'npm:@mescius/spread-sheets-pdf/index.js',
'@mescius/spread-sheets-tablesheet': 'npm:@mescius/spread-sheets-tablesheet/index.js',
'@mescius/spread-sheets-vue': 'npm:@mescius/spread-sheets-vue/index.js',
},
meta: {
'*.css': { loader: 'systemjs-plugin-css' },
'*.vue': { loader: "../plugin-vue/index.js" }
}
});
})(this);