The defaultValue API is used to set a default value/formula to a cell, so that when the cell is empty, it will display the default value or the value calculated from a default formula.
When an empty cell has a default value/formula applied to it and a user edits that cell, the default value will not be overridden.
The APIs are as follows:
<template>
<div class="sample-tutorial">
<gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread">
<gc-worksheet></gc-worksheet>
<gc-worksheet />
</gc-spread-sheets>
<div class="options-container">
<div class="option-row">
<p>
<span>You can set, get the default value and formula for the active cells.</span>
</p>
</div>
<div class="option-row">
<label for="value">Default Value: </label>
<input id="value" v-model="value" />
</div>
<div class="option-row">
<label for="formula">Default Formula: </label>
<div id="formula" spellcheck="false" style="border: 1px solid #808080;width:100%;height:21px;overflow:hidden;">
</div>
</div>
<div class="option-row">
<input type="button" id="btnSetDefault" value="Set Default"
title="Set default value and formula to selected item(s)" @click="handleSetDefault" />
</div>
<div class="option-row">
<label for="showDefault">Get Default Value: </label>
<textarea id="showDefault" cols="31" readonly v-model="showDefault"></textarea>
</div>
</div>
</div>
</template>
<script setup>
import '@mescius/spread-sheets-vue'
import "@mescius/spread-sheets-io";
import { ref, computed } from "vue";
import GC from "@mescius/spread-sheets";
const spread = ref(null);
const fbx = ref(null);
const value = ref('');
const showDefault = ref('');
const getFileUrl = () => {
return '$DEMOROOT$/spread/source/data/defaultValue.sjs';
}
const initSpread = (spreadInstance) => {
spread.value = spreadInstance;
fbx.value = new GC.Spread.Sheets.FormulaTextBox.FormulaTextBox(document.getElementById('formula'), { rangeSelectMode: true });
fbx.value.workbook(spreadInstance);
fetch(getFileUrl()).then(res => res.blob()).then(file => {
spreadInstance.open(file, () => {
let sheet = spreadInstance.getActiveSheet();
sheet.suspendPaint();
sheet.suspendCalcService();
for (let i = 0; i < 12; i++) {
sheet.getCell(2, 3 + i, 3).defaultValue('=$B3/12');
sheet.getCell(3, 3 + i, 3).defaultValue('=$B4/12');
}
sheet.resumeCalcService();
sheet.resumePaint();
getDefault(spreadInstance);
});
});
bindEvents(spreadInstance);
};
const bindEvents = (spreadInstance) => {
spreadInstance.bind(GC.Spread.Sheets.Events.SelectionChanged, () => {
getDefault(spreadInstance);
});
spreadInstance.bind(GC.Spread.Sheets.Events.ActiveSheetChanged, () => {
getDefault(spreadInstance);
});
};
const handleSetDefault = () => {
let sheet = spread.value.getActiveSheet();
let val = value.value;
let selections = sheet.getSelections();
if (!selections || selections.length === 0) {
return;
}
sheet.suspendPaint();
let length = selections.length;
for (let i = 0; i < length; i++) {
let sel = selections[i],
rowIndex = sel.row,
colIndex = sel.col,
rowCount = sel.rowCount,
colCount = sel.colCount,
maxRow = rowIndex + rowCount,
maxColumn = colIndex + colCount,
r, c;
if (rowIndex !== -1 && colIndex !== -1) {
for (r = rowIndex; r < maxRow; r++) {
for (c = colIndex; c < maxColumn; c++) {
sheet.setDefaultValue(r, c, fbx.value.text() || convertValue(val));
}
}
}
}
sheet.resumePaint();
getDefault(spread.value);
};
const getDefault = (spreadInstance) => {
let sheet = spreadInstance.getActiveSheet(), selections = sheet.getSelections();
if (!selections || selections.length === 0) {
showDefault.value = '';
return;
}
let sel = selections[0], row = sel.row, col = sel.col;
showDefault.value = sheet.getDefaultValue(row, col) || '';
};
const convertValue = (val) => {
if (!val) {
return val;
}
// try to convert to number
let num = Number(val);
if (!isNaN(num)) {
return num;
}
// try to convert to boolean
if (val.toLowerCase() === "true" || val.toLowerCase() === "false") {
return val.toLowerCase() === "true";
}
// try to convert to date
let date = new Date(val);
if (!isNaN(date.getTime())) {
return date;
}
return val;
};
</script>
<style scoped>
.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: 12px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
}
.option-row {
font-size: 14px;
padding: 5px;
margin-top: 10px;
}
label {
display: inline-block;
margin-bottom: 6px;
}
input {
padding: 4px;
width: 100%;
margin: 0 4px 4px 0;
box-sizing: border-box;
}
input[type=button] {
width: 30%;
}
p {
background-color: #F4F8EB;
padding: 4px;
}
p span {
display: block;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
<!DOCTYPE html>
<html 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$/en/vue3/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/en/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$/en/lib/vue3/license.js');
</script>
</head>
<body>
<div id="app" style="height: 100%;"></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: {
'@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js',
'@mescius/spread-sheets-io': 'npm:@mescius/spread-sheets-io/index.js',
'@mescius/spread-sheets-vue': 'npm:@mescius/spread-sheets-vue/index.js',
'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",
},
meta: {
'*.css': { loader: 'systemjs-plugin-css' },
'*.vue': { loader: "../plugin-vue/index.js" }
}
});
})(this);