WEBSERVICE 函数请求一个允许跨域资源共享(CORS)的网站,最终得到处理成字符串的返回值。
FILTERJSON 函数将一个有效的json字符串转化为一个值、一个对象或者一个可以拆分的对象数组。
WEBSERVICE
如果请求不遵循同源策略的网站,或者请求的网站不允许来自源网站的跨域资源共享,会返回#VALUE!错误。该函数的返回结果是一个字符串类型。
FILTERJSON
FILTERJSON 函数将一个有效的json字符串转化为一个值、一个对象或者一个可以拆分的对象数组。
您可以同时使用FILTERJSON函数以及WEBSERVICE函数从服务器上获取json对象并在SpreadJS中使用。
window.onload = function() {
var spread = new GC.Spread.Sheets.Workbook(_getElementById("ss"));
spread.options.allowDynamicArray = true;
initSpread(spread);
};
function initSpread(spread) {
spread.setSheetCount(1);
spread.suspendPaint();
spread.suspendCalcService();
initSheet1(spread.getSheet(0));
spread.resumeCalcService();
spread.resumePaint();
}
function initSheet1(sheet) {
sheet.suspendPaint();
sheet.name('WebService');
sheet.setColumnWidth(0,27);
sheet.setColumnWidth(1,300);
sheet.setColumnWidth(2,168);
sheet.setColumnWidth(3,168);
sheet.setValue(0, 1, 'Enter full or partial country name');
var inputStyle = new GC.Spread.Sheets.Style();
inputStyle.backColor = "#FEF3CD";
sheet.setStyle(1,1,inputStyle);
sheet.setValue(3,1,"Name");
sheet.setValue(3,2,"Population");
sheet.setValue(3,3,"Area");
sheet.setValue(1,1,"united");
var headerStyle = new GC.Spread.Sheets.Style();
headerStyle.backColor = "rgb(222,235,246)";
headerStyle.font = "bold 11pt Calibri";
headerStyle.hAlign = 1;
var borderBottom = new GC.Spread.Sheets.LineBorder;
borderBottom.color = "black";
borderBottom.style = GC.Spread.Sheets.LineStyle.thin;
headerStyle.borderBottom = borderBottom;
sheet.setStyle(3,1,headerStyle);
sheet.setStyle(3,2,headerStyle);
sheet.setStyle(3,3,headerStyle);
for (var r = 4; r < 50; r++) {
//show the spilled objects as strings by using formatter property
sheet.setFormatter(r,1,'=iferror(property(@,"name"), "")');
sheet.setFormatter(r,2,'#,##0');
sheet.setFormatter(r,3,'#,##0');
}
sheet.setFormula(4,1,'=FILTERJSON(IF(LEN(B2)>3,WEBSERVICE("https://restcountries.com/v2/name/"&B2)))');
sheet.setFormula(4,2,'=IFERROR(PROPERTY(B5#,"population"),"")');
sheet.setFormula(4,3,'=IFERROR(PROPERTY(B5#,"area"),"")');
sheet.resumePaint();
}
function _getElementById(id) {
return document.getElementById(id);
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta name="spreadjs culture" content="zh-cn" />
<meta charset="utf-8" />
<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$/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>
</body>
</html>
input[type="text"] {
width: 200px;
margin-right: 20px;
}
label {
display: inline-block;
width: 110px;
}
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: 100%;
height: 100%;
overflow: hidden;
float: left;
}
label {
display: block;
margin-bottom: 6px;
}
input {
padding: 4px 6px;
}
input[type=button] {
margin-top: 6px;
display: block;
width:216px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
code {
border: 1px solid #000;
}