自定义替换文字

除了标准的单元格文本外,SpreadJS还支持为浮动对象、图片、图表和形状等图形内容设置自定义的替代(alt)文本。替代文本也可以包含用于单元格值的占位符。例如,如果单元格的值为1000,替代文本为"销售金额为{value}",最终的可访问内容将是"销售金额为1000"。

SpreadJS将单元格类型文本、单元格文本、单元格公式和单元格注释视为可选文本,这可以适应大多数情况。 但对于一些特殊情况,比如单元格中的图标,用户可以设置单元格的替代文本。 类GC.Spread.Sheets.Worksheet具有方法setAltText和getAltText。 GC.Spread.Sheets.CellRange类具有方法altText。 对于包括图片、图表和形状在内的图形内容,SpreadJS无法提供适当的替代文本,但用户可以自定义。 GC.Spread.Sheets.FloatingObjects.Picture类具有方法alt。 GC.Spread.Sheets.Charts.Chart类中的alt方法, GC.Spread.Sheets.Shapes.Shape类,GC.Spread.Sheets.Shapes.ConnectorShape类以及GC.Spread.Sheets.Shapes.GroupShape类中的alt方法。
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(_getElementById('ss'), { sheetCount: 1 }); initSpread(spread); }; function initSpread(spread) { spread.suspendPaint(); //enable accessibility spread.options.enableAccessibility = true; //set focus spread.focus(); //change the commands related to Tab key, Shift key with Tab key var commands = spread.commandManager(); //TAB commands.register("moveToNextCellThenControl", GC.Spread.Sheets.Commands.moveToNextCellThenControl, GC.Spread.Commands.Key.tab, false, false, false, false); //SHIFT+TAB commands.register("moveToPreviousCellThenControl", GC.Spread.Sheets.Commands.moveToPreviousCellThenControl, GC.Spread.Commands.Key.tab, false, true, false, false); var sheet = spread.getActiveSheet(); //set default row height and column width sheet.defaults.rowHeight = 50; sheet.defaults.colWidth = 150; //set default horizontal alignment and vertical alignment var defaultStyle = sheet.getDefaultStyle(); defaultStyle.hAlign = GC.Spread.Sheets.HorizontalAlign.center; defaultStyle.vAlign = GC.Spread.Sheets.VerticalAlign.center; sheet.setDefaultStyle(defaultStyle); //bind data source sheet.setDataSource(dataSource); var p1 = sheet.pictures.add("p1", "$DEMOROOT$/spread/source/images/apple.jpg", 50, 50, 200, 200); p1.alt("this is a apple image"); p1.borderColor("rgba(20, 119, 167, 1)"); p1.borderWidth(2); p1.borderStyle("solid"); var c1 = sheet.charts.add("c1", GC.Spread.Sheets.Charts.ChartType.columnCluster, 50, 300, 400, 400, "C1:D5"); c1.alt("this is a column chart"); var chartArea = c1.chartArea(); chartArea.border.color = "rgba(20, 119, 167, 1)"; chartArea.border.width = 2; c1.chartArea(chartArea); var sp1 = sheet.shapes.add("sp1", GC.Spread.Sheets.Shapes.AutoShapeType.sun, 300, 50, 200, 200); sp1.alt("this is a sun shape"); spread.resumePaint(); //bind events to set alternative text function setAltText(collection, altText) { var success = false; collection.forEach(function (obj) { if (obj.isSelected()) { obj.alt(altText); success = true; } }); return success; } var alternativeText = document.getElementById("alternativeText"); document.getElementById("setAlternativeText").addEventListener("click", function () { var altText = alternativeText.value; var success = setAltText(sheet.pictures.all(), altText); if (success) { return; } success = setAltText(sheet.charts.all(), altText); if (success) { return; } setAltText(sheet.shapes.all(), altText); }); spread.bind(GC.Spread.Sheets.Events.PictureChanged, function (event, args) { if (args.propertyName === "isSelected" && args.picture.isSelected()) { alternativeText.value = args.picture.alt(); } }); spread.bind(GC.Spread.Sheets.Events.FloatingObjectChanged, function (event, args) { var floatingObject = args.floatingObject; if (floatingObject && floatingObject instanceof GC.Spread.Sheets.Charts.Chart) { if (args.propertyName === "isSelected" && floatingObject.isSelected()) { alternativeText.value = floatingObject.alt(); } } }); spread.bind(GC.Spread.Sheets.Events.ShapeChanged, function (event, args) { if (args.propertyName === "isSelected" && args.shape.isSelected()) { alternativeText.value = args.shape.alt(); } }); var cellAlternativeText = document.getElementById("cellAlternativeText"); spread.bind(GC.Spread.Sheets.Events.SelectionChanged, function (event, args) { var sheet = args.sheet; cellAlternativeText.value = sheet.getAltText(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()) || ""; }); document.getElementById("setCellAlternativeText").addEventListener("click", function () { sheet.setAltText(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), cellAlternativeText.value); }); } function _getElementById(id) { return document.getElementById(id); }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta name="spreadjs culture" content="zh-cn" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-shapes/dist/gc.spread.sheets.shapes.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-charts/dist/gc.spread.sheets.charts.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/data/data.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.min.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> <div class="options-container"> <div class="option-row"> <textarea type="text" id="alternativeText" value=""></textarea> <input type="button" id="setAlternativeText" value="Set Alternative Text" /> <textarea type="text" id="cellAlternativeText" value=""></textarea> <input type="button" id="setCellAlternativeText" value="Set Cell Alternative Text" /> </div> </div> </div> </body> </html>
.sample-tutorial { 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; } #alternativeText { width: 240px; height: 120px; } #cellAlternativeText { width: 240px; height: 120px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }