Read-only
The example below defines a spreadsheet column or a single cell as read-only.
JavaScript example
<script src="https://jspreadsheet.com/v10/jspreadsheet.js"></script>
<script src="https://jsuites.net/v5/jsuites.js"></script>
<link rel="stylesheet" href="https://jspreadsheet.com/v10/jspreadsheet.css" type="text/css" />
<link rel="stylesheet" href="https://jsuites.net/v5/jsuites.css" type="text/css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons" />
<div id="spreadsheet"></div>
<p><input type="button" value="Disabled B2" onclick="toggle(this)"></p>
<script>
var toggle = function(b) {
if (worksheets[0].isReadOnly('B2')) {
worksheets[0].setReadOnly('B2', false);
b.value = 'Disable B2';
} else {
worksheets[0].setReadOnly('B2', true);
b.value = 'Enable B2';
}
}
// Set your JSS license key (The following key only works for one day)
jspreadsheet.setLicense('YzdhYzJjMmE3OWZiMjczZDBjNDY2MGFkZjdiMGI3YTM4NWFhODg3ZmI0OTU2YWU0MTRmODE4NDkwY2Y2YWVmZGRkYjEyYjlkZjNiYWNiMGVlY2UxOGRmNGE3NTE4MDJkOTc0YWFiNDEzOWIzYTU2MWRhZjM3YmMwMzY5MTBkYTQsZXlKdVlXMWxJam9pU25Od2NtVmhaSE5vWldWMElpd2laR0YwWlNJNk1UWTROVGczTkRrek1pd2laRzl0WVdsdUlqcGJJbXB6Y0hKbFlXUnphR1ZsZEM1amIyMGlMQ0pqYjJSbGMyRnVaR0p2ZUM1cGJ5SXNJbXB6YUdWc2JDNXVaWFFpTENKamMySXVZWEJ3SWl3aWQyVmlJaXdpYkc5allXeG9iM04wSWwwc0luQnNZVzRpT2lJek5DSXNJbk5qYjNCbElqcGJJblkzSWl3aWRqZ2lMQ0oyT1NJc0luWXhNQ0lzSW1Ob1lYSjBjeUlzSW1admNtMXpJaXdpWm05eWJYVnNZU0lzSW5CaGNuTmxjaUlzSW5KbGJtUmxjaUlzSW1OdmJXMWxiblJ6SWl3aWFXMXdiM0owSWl3aVltRnlJaXdpZG1Gc2FXUmhkR2x2Ym5NaUxDSnpaV0Z5WTJnaUxDSndjbWx1ZENJc0luTm9aV1YwY3lKZExDSmtaVzF2SWpwMGNuVmxmUT09');
// Create a new spreadsheet
var worksheets = jspreadsheet(document.getElementById('spreadsheet'), {
worksheets: [{
data: [
['Mazda', 2001, 2000, 1],
['Peugeot', 2010, 5000, 1],
['Honda Fit', 2009, 3000, 1],
['Honda CRV', 2010, 6000, 0],
],
columns: [
{
type: 'text',
title:'Description',
width:'200px',
readOnly:true,
},
{
type: 'text',
title:'Year',
width:'200px'
},
{
type: 'text',
title:'Price',
width:'100px',
mask:'#.##',
},
{
type: 'checkbox',
title:'Automatic',
width:'100px'
},
],
}],
oncreatecell: (worksheet, cell, x, y, source, value, label) => {
if (x == 2 && y == 2) {
cell.classList.add('readonly');
}
}
});
</script>
</html>
React example
import React, { useRef } from "react";
import { Spreadsheet, Worksheet } from "@jspreadsheet/react";
// Set the license
const license = 'YzdhYzJjMmE3OWZiMjczZDBjNDY2MGFkZjdiMGI3YTM4NWFhODg3ZmI0OTU2YWU0MTRmODE4NDkwY2Y2YWVmZGRkYjEyYjlkZjNiYWNiMGVlY2UxOGRmNGE3NTE4MDJkOTc0YWFiNDEzOWIzYTU2MWRhZjM3YmMwMzY5MTBkYTQsZXlKdVlXMWxJam9pU25Od2NtVmhaSE5vWldWMElpd2laR0YwWlNJNk1UWTROVGczTkRrek1pd2laRzl0WVdsdUlqcGJJbXB6Y0hKbFlXUnphR1ZsZEM1amIyMGlMQ0pqYjJSbGMyRnVaR0p2ZUM1cGJ5SXNJbXB6YUdWc2JDNXVaWFFpTENKamMySXVZWEJ3SWl3aWQyVmlJaXdpYkc5allXeG9iM04wSWwwc0luQnNZVzRpT2lJek5DSXNJbk5qYjNCbElqcGJJblkzSWl3aWRqZ2lMQ0oyT1NJc0luWXhNQ0lzSW1Ob1lYSjBjeUlzSW1admNtMXpJaXdpWm05eWJYVnNZU0lzSW5CaGNuTmxjaUlzSW5KbGJtUmxjaUlzSW1OdmJXMWxiblJ6SWl3aWFXMXdiM0owSWl3aVltRnlJaXdpZG1Gc2FXUmhkR2x2Ym5NaUxDSnpaV0Z5WTJnaUxDSndjbWx1ZENJc0luTm9aV1YwY3lKZExDSmtaVzF2SWpwMGNuVmxmUT09';
// Readonly state
const toggle = function(worksheet, button) {
if (worksheet.isReadOnly('B2')) {
worksheet.setReadOnly('B2', false);
button.value = 'Disable B2';
} else {
worksheet.setReadOnly('B2', true);
button.value = 'Enable B2';
}
}
// Create the component
export default function App() {
// Spreadsheet array of worksheets
const spreadsheet = useRef();
// Data
const data = [
['Mazda', 2001, 2000, 1],
['Peugeot', 2010, 5000, 1],
['Honda Fit', 2009, 3000, 1],
['Honda CRV', 2010, 6000, 0],
]
// Columns
const columns = [
{
type: 'text',
title:'Description',
width:'200px',
readOnly:true,
},
{
type: 'text',
title:'Year',
width:'200px'
},
{
type: 'text',
title:'Price',
width:'100px',
mask:'#.##',
},
{
type: 'checkbox',
title:'Automatic',
width:'100px'
},
]
// Event
const oncreatecell = (worksheet, cell, x, y, source, value, label) => {
if (x == 2 && y == 2) {
cell.classList.add('readonly');
}
}
// Method
const toggle = function(worksheet, button) {
if (worksheet.isReadOnly('B2')) {
worksheet.setReadOnly('B2', false);
button.value = 'Disable B2';
} else {
worksheet.setReadOnly('B2', true);
button.value = 'Enable B2';
}
}
// Render data grid component
return (
<>
<Spreadsheet ref={spreadsheet} license={license} toolbar oncreatecell={oncreatecell}>
<Worksheet data={data} columns={columns} />
</Spreadsheet>
<input type={"button"} value={"Disabled B2"} onClick={() => toggle(spreadsheet.current[0], this)} />
</>
);
}
Vue example
<template>
<Spreadsheet ref="spreadsheet" :license="license" :oncreatecell="oncreatecell">
<Worksheet :data="data" :columns="columns" />
</Spreadsheet>
<input ref="button" type="button" value="Disabled B2" @click="toggle" />
</template>
<script>
import { Spreadsheet, Worksheet } from "@jspreadsheet/vue";
// Set the license
const license = 'YzdhYzJjMmE3OWZiMjczZDBjNDY2MGFkZjdiMGI3YTM4NWFhODg3ZmI0OTU2YWU0MTRmODE4NDkwY2Y2YWVmZGRkYjEyYjlkZjNiYWNiMGVlY2UxOGRmNGE3NTE4MDJkOTc0YWFiNDEzOWIzYTU2MWRhZjM3YmMwMzY5MTBkYTQsZXlKdVlXMWxJam9pU25Od2NtVmhaSE5vWldWMElpd2laR0YwWlNJNk1UWTROVGczTkRrek1pd2laRzl0WVdsdUlqcGJJbXB6Y0hKbFlXUnphR1ZsZEM1amIyMGlMQ0pqYjJSbGMyRnVaR0p2ZUM1cGJ5SXNJbXB6YUdWc2JDNXVaWFFpTENKamMySXVZWEJ3SWl3aWQyVmlJaXdpYkc5allXeG9iM04wSWwwc0luQnNZVzRpT2lJek5DSXNJbk5qYjNCbElqcGJJblkzSWl3aWRqZ2lMQ0oyT1NJc0luWXhNQ0lzSW1Ob1lYSjBjeUlzSW1admNtMXpJaXdpWm05eWJYVnNZU0lzSW5CaGNuTmxjaUlzSW5KbGJtUmxjaUlzSW1OdmJXMWxiblJ6SWl3aWFXMXdiM0owSWl3aVltRnlJaXdpZG1Gc2FXUmhkR2x2Ym5NaUxDSnpaV0Z5WTJnaUxDSndjbWx1ZENJc0luTm9aV1YwY3lKZExDSmtaVzF2SWpwMGNuVmxmUT09';
export default {
components: {
Spreadsheet,
Worksheet,
},
methods() {
toggle() {
let worksheet = this.refs.spreadsheet.current[0];
if (worksheet.isReadOnly('B2')) {
worksheet.setReadOnly('B2', false);
this.refs.button.value.value = 'Disable B2';
} else {
worksheet.setReadOnly('B2', true);
this.refs.button.value.value = 'Enable B2';
}
}
},
methods: {
// Event
oncreatecell(worksheet, cell, x, y, source, value, label) {
if (x == 2 && y == 2) {
cell.classList.add('readonly');
}
}
},
data() {
// Data
const data = [
['Mazda', 2001, 2000, 1],
['Peugeot', 2010, 5000, 1],
['Honda Fit', 2009, 3000, 1],
['Honda CRV', 2010, 6000, 0],
]
// Columns
const columns = [
{
type: 'text',
title:'Description',
width:'200px',
readOnly:true,
},
{
type: 'text',
title:'Year',
width:'200px'
},
{
type: 'text',
title:'Price',
width:'100px',
mask:'#.##',
},
{
type: 'checkbox',
title:'Automatic',
width:'100px'
},
]
return {
data,
columns,
license,
};
}
}
</script>
Angular example
import { Component, ViewChild, ElementRef } from "@angular/core";
import * as jspreadsheet from "jspreadsheet";
// Set your JSS license key (The following key only works for one day)
jspreadsheet.setLicense('YzdhYzJjMmE3OWZiMjczZDBjNDY2MGFkZjdiMGI3YTM4NWFhODg3ZmI0OTU2YWU0MTRmODE4NDkwY2Y2YWVmZGRkYjEyYjlkZjNiYWNiMGVlY2UxOGRmNGE3NTE4MDJkOTc0YWFiNDEzOWIzYTU2MWRhZjM3YmMwMzY5MTBkYTQsZXlKdVlXMWxJam9pU25Od2NtVmhaSE5vWldWMElpd2laR0YwWlNJNk1UWTROVGczTkRrek1pd2laRzl0WVdsdUlqcGJJbXB6Y0hKbFlXUnphR1ZsZEM1amIyMGlMQ0pqYjJSbGMyRnVaR0p2ZUM1cGJ5SXNJbXB6YUdWc2JDNXVaWFFpTENKamMySXVZWEJ3SWl3aWQyVmlJaXdpYkc5allXeG9iM04wSWwwc0luQnNZVzRpT2lJek5DSXNJbk5qYjNCbElqcGJJblkzSWl3aWRqZ2lMQ0oyT1NJc0luWXhNQ0lzSW1Ob1lYSjBjeUlzSW1admNtMXpJaXdpWm05eWJYVnNZU0lzSW5CaGNuTmxjaUlzSW5KbGJtUmxjaUlzSW1OdmJXMWxiblJ6SWl3aWFXMXdiM0owSWl3aVltRnlJaXdpZG1Gc2FXUmhkR2x2Ym5NaUxDSnpaV0Z5WTJnaUxDSndjbWx1ZENJc0luTm9aV1YwY3lKZExDSmtaVzF2SWpwMGNuVmxmUT09');
@Component({
selector: "app-root",
template: `
<div #spreadsheet></div>
<input #button type='button' value='Disabled B2' (click)="this.toggle()" />`;
})
export class AppComponent {
@ViewChild("spreadsheet") spreadsheet: ElementRef;
@ViewChild("button") button: ElementRef;
// Worksheets
worksheets: jspreadsheet.worksheetInstance[];
// Data
data: data;
// Create a new data grid
ngOnInit() {
// Create spreadsheet
this.worksheets = jspreadsheet(this.spreadsheet.nativeElement, {
worksheets: [{
data: [
['Mazda', 2001, 2000, 1],
['Peugeot', 2010, 5000, 1],
['Honda Fit', 2009, 3000, 1],
['Honda CRV', 2010, 6000, 0],
],
columns: [
{
type: 'text',
title:'Description',
width:'200px',
readOnly:true,
},
{
type: 'text',
title:'Year',
width:'200px'
},
{
type: 'text',
title:'Price',
width:'100px',
mask:'#.##',
},
{
type: 'checkbox',
title:'Automatic',
width:'100px'
},
],
}],
oncreatecell: (worksheet, cell, x, y, source, value, label) => {
if (x == 2 && y == 2) {
cell.classList.add('readonly');
}
}
});
}
toggle() {
if (this.worksheets[0].isReadOnly('B2')) {
this.worksheets[0].setReadOnly('B2', false);
this.button.value = 'Disable B2';
} else {
this.worksheets[0].setReadOnly('B2', true);
this.button.value = 'Enable B2';
}
}
}