Cell

SpreadJS supports two-way binding to JavaScript objects. This sample demonstrates modifying and updating a JavaScript object using bound cells and HTML input controls.

Description
app.vue
index.html
Copy to CodeMine

You can create a cell binding source, and then use the setBindingPath method to set the binding path for cell-level binding in a specified cell in the specified sheet area. Then set the data source for the sheet. For example:

    var person = { name: 'Wang feng', age: 25, sex: 'male', address: { postcode: '710075' } };
    var source = new GC.Spread.Sheets.Bindings.CellBindingSource(person);
    sheet.setBindingPath(2, 1, 'name');
    sheet.setBindingPath(3, 1, 'age');
    sheet.setBindingPath(4, 1, 'sex');
    sheet.setBindingPath(5, 1, 'address.postcode');
    sheet.setDataSource(source);

You can use getBindingPath to get the binding path of cell-level binding from the specified cell in the specified sheet area.

You can create a cell binding source, and then use the setBindingPath method to set the binding path for cell-level binding in a specified cell in the specified sheet area. Then set the data source for the sheet. For example: You can use getBindingPath to get the binding path of cell-level binding from the specified cell in the specified sheet area.
<template> <div class="sample-tutorial"> <gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread"> <gc-worksheet> <gc-column :width=120></gc-column> <gc-column :width=120></gc-column> <gc-column :width=120></gc-column> </gc-worksheet> </gc-spread-sheets> <div class="options-container"> <div class="option-row"> <label style="background-color:#F4F8EB;">Select one of the gray cells to get its binding. You can also change the Person object below to see it change in the Spread instance: change any of the text in the text boxes below.</label> </div> <div class="option-row"> <label>Name:&nbsp;&nbsp;</label> <input type="text" v-on:keyup="updateName" :value="personRef.name" /> <label>Age:&nbsp;&nbsp;</label> <input type="text" v-on:keyup="updateAge" :value="personRef.age" /> <label>Sex:&nbsp;&nbsp;</label> <input type="text" v-on:keyup="updateSex" :value="personRef.sex" /> <label>Postcode:&nbsp;&nbsp;</label> <input type="text" v-on:keyup="updatePostcode" :value="personRef.address.postcode" /> <label>GetBindingPath:&nbsp;&nbsp;</label> <label style="color:darkgreen">{{ pathRef }}</label> </div> </div> </div> </template> <script setup> import '@mescius/spread-sheets-vue'; import { ref } from "vue"; import GC from "@mescius/spread-sheets"; const defaultPerson = { name: "Wang Feng", age: 25, sex: "Male", address: { postcode: "710075" } }; const spreadRef = ref(null); const sheetRef = ref(null); const personRef = ref(defaultPerson); const pathRef = ref(''); const updateName = (value) => { if (personRef.value && sheetRef) { personRef.value.name = value.target.value; sheetRef.value.repaint(); } }; const updateAge = (value) => { if (personRef.value && sheetRef.value) { personRef.value.age = isNaN(parseInt(value.target.value)) ? undefined : parseInt(value.target.value); sheetRef.value.repaint(); } }; const updateSex = (value) => { if (personRef.value && sheetRef.value) { personRef.value.sex = value.target.value; sheetRef.value.repaint(); } }; const updatePostcode = (value) => { if (personRef.value && sheetRef.value) { personRef.value.address.postcode = value.target.value; sheetRef.value.repaint(); } }; const initSpread = (spread) => { spreadRef.value = spread; let spreadNS = GC.Spread.Sheets; sheet = spread.sheets[0]; sheet.suspendPaint(); sheet.setValue(0, 0, 'cell-binding'); sheet.setValue(2, 1, 'Name'); sheet.setValue(3, 1, 'Age'); sheet.setValue(4, 1, 'Sex'); sheet.setValue(5, 1, 'Address.postcode'); let source = new spreadNS.Bindings.CellBindingSource(personRef.value); sheet.setBindingPath(2, 2, "name"); sheet.setBindingPath(3, 2, "age"); sheet.setBindingPath(4, 2, "sex"); sheet.setBindingPath(5, 2, "address.postcode"); sheet.setDataSource(source); sheet.setSelection(2, 2, 1, 1); let path = sheet.getBindingPath(2, 2); sheet.getRange(2, 2, 4, 1).backColor("rgb(208,206,206)"); pathRef.value = path || ""; sheet.bind(spreadNS.Events.SelectionChanged, function () { let activeCell = sheet.getSelections()[0]; let path = sheet.getBindingPath(activeCell.row, activeCell.col); pathRef.value = path || ""; }); sheet.bind(spreadNS.Events.CellChanged, function () { personRef.value.age = defaultPerson.age || 0; personRef.value.name = defaultPerson.name || ""; personRef.value.postcode = defaultPerson.address.postcode || ""; personRef.value.sex = defaultPerson.sex || ""; }); sheet.resumePaint(); sheetRef.value = sheet; }; </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: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .option-row { font-size: 14px; padding: 5px; margin-top: 10px; } label { display: block; margin-bottom: 6px; } input { padding: 4px 6px; } input[type=button] { margin-top: 6px; display: block; } input[type=text] { margin-top: 6px; margin-bottom: 6px; 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"></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-en': 'npm:@mescius/spread-sheets-resources-en/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);