要创建一个文件上传单元格,请按照以下示例操作:
您可以使用valuePath属性自定义单元格值路径。默认值为'dataUrl'。
您可以使用提供的许多API自定义按钮。如果您想要控制按钮在单元格中的位置,请使用以下方法。
marginTop:获取或设置按钮相对于单元格的顶部边距(以像素为单位)。
marginRight:获取或设置按钮相对于单元格的右侧边距(以像素为单位)。
marginBottom:获取或设置按钮相对于单元格的底部边距(以像素为单位)。
marginLeft:获取或设置按钮相对于单元格的左侧边距(以像素为单位)。
您可以使用maxSize方法获取并设置可上传文件的大小限制。使用accept方法获取并设置可上传文件的类型限制。例如
如果您想要控制单元格中文件上传按钮的操作列表,请使用以下方法。
isPreviewEnabled:获取或设置是否显示文件预览按钮。
isDownloadEnabled:获取或设置是否显示文件下载按钮。
isClearEnabled:获取或设置是否显示文件清除按钮。
您可以使用previewCommand属性自定义预览逻辑。
最后,您可以使用builtInFileIcons属性为不同的文件类型自定义图标。
import { builtInFileIcons, demoFiles } from './utils.js';
var spreadNS = GC.Spread.Sheets;
window.onload = function () {
const spread = new spreadNS.Workbook(document.getElementById("ss"), { sheetCount: 1 });
initSpread(spread);
};
function initSpread(spread) {
spread.suspendPaint();
const sheet = spread.getActiveSheet();
initLayout(sheet);
// set cell type
const fileUpload1 = new spreadNS.CellTypes.FileUpload();
fileUpload1.accept('.jpg,.png,.gif');
fileUpload1.valuePath(undefined);
sheet.getCell(1, 0).cellType(fileUpload1);
const fileUpload2 = new spreadNS.CellTypes.FileUpload();
fileUpload2.isDownloadEnabled(false);
fileUpload2.isClearEnabled(false);
fileUpload2.valuePath(undefined);
sheet.getCell(1, 1).cellType(fileUpload2);
const fileUpload3 = new spreadNS.CellTypes.FileUpload();
sheet.getCell(1, 4).cellType(fileUpload3);
fileUpload3.valuePath(undefined);
sheet.getCell(2, 4).cellType(fileUpload3);
const fileUpload4 = new spreadNS.CellTypes.FileUpload();
fileUpload4.isDownloadEnabled(false);
fileUpload4.valuePath(undefined);
sheet.getCell(1, 5).cellType(fileUpload4);
const fileUpload5 = new spreadNS.CellTypes.FileUpload();
fileUpload5.isClearEnabled(false);
fileUpload5.valuePath(undefined);
sheet.getCell(2, 5).cellType(fileUpload5);
// set cell value
sheet.setValue(1, 0, demoFiles.image);
sheet.setValue(1, 1, demoFiles.image);
sheet.setValue(1, 4, demoFiles.ssjson);
sheet.setValue(2, 4, demoFiles.json);
sheet.setValue(1, 5, demoFiles.js);
sheet.setValue(2, 5, demoFiles.xlsx);
// set custom file icons
spread.options.builtInFileIcons = builtInFileIcons;
spread.resumePaint();
};
function initLayout(sheet) {
// Description
sheet.addSpan(0, 0, 1, 3, spreadNS.SheetArea.viewport);
sheet.addSpan(0, 4, 1, 7, spreadNS.SheetArea.viewport);
sheet.setColumnWidth(0, 70);
sheet.setColumnWidth(1, 70);
sheet.setColumnWidth(2, 200);
sheet.setColumnWidth(4, 100);
sheet.setColumnWidth(5, 100);
sheet.setRowHeight(0, 50);
sheet.setRowHeight(1, 70);
sheet.setRowHeight(2, 70);
sheet.setDefaultValue(0, 0, '1. Upload image files and customize file operations.');
sheet.setDefaultValue(0, 4, '2. Upload other non-image format files and customize the icons of related files.');
const style = new spreadNS.Style();
style.font = 'bold Calibri 14px';
style.vAlign = spreadNS.VerticalAlign.center;
sheet.getRange(0, 0, 1, 11).setStyle(style);
const style2 = new spreadNS.Style();
style2.vAlign = spreadNS.VerticalAlign.center;
style2.hAlign = spreadNS.HorizontalAlign.center;
sheet.getRange(1, 4, 2, 2).setStyle(style2);
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="spreadjs culture" content="zh-cn" />
<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-resources-zh/dist/gc.spread.sheets.resources.zh.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script>
<script type="module" 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" style="width:100%; height: 100%"></div>
</div>
</body>
</html>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}