语法
MAP函数的语法有以下参数。
array1
一个要被映射的数组。
lambdaorarray<#>
一个LAMBDA,它必须是最后一个参数,它必须为每个传递的数组提供一个参数,或者是另一个要被映射的数组。
备注
在使用这个函数之前,你需要打开allowDynamicArray选项
示例
超过一定数值的平方数
从同一行的两列中找出都是 "真 "的值
var data = {
values: {
1: { 1: "Examples" },
3: { 1: "Example 1: Square numbers above a vertain value" },
4: { 1: "Data:", 5: "Formula:" },
5: { 1: 1, 2: 2, 3: 3, 5: "Result:" },
6: { 1: 4, 2: 5, 3: 6 },
7: {
5: "Calculating the last LAMBDA parameter as a*1 coerces it to an integer and prevents a #CALC! error.",
},
10: { 1: "Example 2: Find values from two columns in one row that are both TRUE" },
11: { 1: "TableA:" },
12: { 1: "Col1", 2: "Col2", 5: "Formula:" },
13: { 1: false, 2: true, 5: "Result:" },
14: { 1: false, 2: true },
15: { 1: true, 2: true },
16: { 1: false, 2: false },
17: { 1: false, 2: true },
18: { 1: true, 2: false },
19: { 1: true, 2: true },
20: { 1: true, 2: false },
23: { 1: "Example 3: Find values from two columns in one row that meet certain criteria" },
24: { 1: "TableB:" },
25: { 1: "Size", 2: "Color", 5: "Formula:" },
26: { 1: "Medium", 2: "Red" },
27: { 1: "Large", 2: "Green", 6: "Valid Size", 7: "Valid Color" },
28: { 1: "Medium", 2: "Blue", 5: "Result:" },
29: { 1: "Medium", 2: "Blue" },
30: { 1: "Medium", 2: "Red" },
31: { 1: "Large", 2: "Red" },
32: { 1: "Medium", 2: "Blue" },
33: { 1: "Small", 2: "Blue" },
34: { 1: "Medium", 2: "Green" },
35: { 1: "Large", 2: "Green" },
},
formulas: {
4: { 6: "FORMULATEXT(G6)" },
5: { 6: "MAP(B6:D7,LAMBDA(a,IF(a>4,a*a,a*1)))" },
12: { 6: "FORMULATEXT(G14)" },
13: { 6: "MAP(TableA[Col1],TableA[Col2],LAMBDA(a,b,AND(a,b)))" },
25: { 6: "FORMULATEXT(G29)" },
28: { 6: 'FILTER(TableB,MAP(TableB[Size],TableB[Color],LAMBDA(s,c,AND(s="Large",c="Red"))))' },
},
cellStyles: {
"B2:C2": 0,
"B4:G4": 1,
B5: 2,
C5: 3,
D5: 4,
"F5:F6": 2,
"G5:I7": 5,
"J5:K5": 5,
"B6:D7": 6,
"F8:K9": 7,
"B11:I11": 1,
"B12:C12": 8,
"F13:F14": 2,
"G13:G21": 5,
"H13:L13": 5,
"B24:J24": 1,
"B25:C25": 8,
F26: 2,
"G26:K27": 9,
"G28:H28": 2,
F29: 2,
"G29:H29": 5,
},
styles: {
records: [
{ foreColor: 0, font: 0, border: [null, null, 0] },
{ foreColor: 0, font: 1, border: [null, null, 1] },
{ backColor: 2, foreColor: 1, font: 1, border: [2, null, 3, 4] },
{ backColor: 2, foreColor: 1, font: 1, border: [2, null, 3] },
{ backColor: 2, foreColor: 1, font: 1, border: [2, 4, 3] },
{ backColor: 4, foreColor: 3, font: 1, border: [3, 3, 3, 3] },
{ backColor: 6, foreColor: 5, border: [3, 3, 3, 3] },
{ backColor: 7, wordWrap: true, border: [5, 5, 5, 5] },
{ foreColor: 0, font: 1 },
{ backColor: 4, foreColor: 3, font: 1, wordWrap: true, border: [3, 3, 3, 3] },
],
borders: [
{ color: "#accdea", style: 5 },
{ color: "#9bc3e6", style: 2 },
{ color: "#000000", style: 1 },
{ color: "#7f7f7f", style: 1 },
{ color: "#9bc3e6", style: 1 },
{ color: "#b2b2b2", style: 1 },
],
colors: ["#44546a", "#ffffff", "#5b9bd5", "#fa7d00", "#f2f2f2", "#3f3f76", "#ffcc99", "#ffffcc"],
fonts: ["700 17.3px Calibri", "700 14.7px Calibri"],
},
others: {
columnWidth: { 0: 30 },
rowHeight: { 1: 24, 2: 21, 3: 21, 10: 21, 23: 21 },
spans: ["B5:D5", "F8:K9", "G26:K27"],
tables: [
{ name: "TableA", ref: "B13:C21" },
{ name: "TableB", ref: "B26:C36" },
],
},
};
window.onload = function () {
var workbook = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
workbook.options.allowDynamicArray = true;
workbook.suspendPaint();
initSheet1(workbook.getSheet(0));
workbook.resumePaint();
};
function initSheet1(sheet) {
setSheetPr(sheet);
setCells(sheet);
}
function setCells(sheet) {
foreachObj(data.values, function (r, row) {
foreachObj(row, function (c, v) {
setValue(sheet, Number(r), Number(c), v);
});
});
foreachObj(data.formulas, function (r, row) {
foreachObj(row, function (c, v) {
setFormula(sheet, Number(r), Number(c), v);
});
});
foreachObj(data.cellStyles, function (ref, id) {
setStyle(sheet, ref, data.styles.records[id]);
});
}
function setValue(sheet, r, c, v) {
if (v === undefined || v === null) return;
sheet.setValue(r, c, v);
}
function setFormula(sheet, r, c, v) {
if (v === undefined || v === null) return;
sheet.setFormula(r, c, v);
}
function setStyle(sheet, ref, v) {
if (v === undefined || v === null) return;
var styles = data.styles;
var range = sheet.getRange(ref);
var foreColor = styles.colors[v.foreColor];
var backColor = styles.colors[v.backColor];
var font = styles.fonts[v.font];
var wordWrap = v.wordWrap;
if (foreColor) {
range.foreColor(foreColor);
}
if (backColor) {
range.backColor(backColor);
}
if (font) {
range.font(font);
}
if (wordWrap) {
range.wordWrap(wordWrap);
}
var border = v.border || [];
var borderTop = styles.borders[border[0]];
var borderRight = styles.borders[border[1]];
var borderBottom = styles.borders[border[2]];
var borderLeft = styles.borders[border[3]];
if (borderTop) {
range.borderTop(createLineStyle(borderTop));
}
if (borderBottom) {
range.borderBottom(createLineStyle(borderBottom));
}
if (borderLeft) {
range.borderLeft(createLineStyle(borderLeft));
}
if (borderRight) {
range.borderRight(createLineStyle(borderRight));
}
}
function setSheetPr(sheet) {
// set column width
foreachObj(data.others.columnWidth, function (index, v) {
sheet.setColumnWidth(Number(index), v);
});
// set row height
foreachObj(data.others.rowHeight, function (index, v) {
sheet.setRowHeight(Number(index), v);
});
// set spans
var spans = data.others.spans || [];
for (var i = 0; i < spans.length; i++) {
var range = sheet.getRange(spans[i]);
sheet.addSpan(range.row, range.col, range.rowCount, range.colCount);
}
// set tables
var tables = data.others.tables || [];
for (var i = 0; i < tables.length; i++) {
var table = tables[i];
var range = sheet.getRange(table.ref);
sheet.tables.add(table.name, range.row, range.col, range.rowCount, range.colCount);
}
}
function createLineStyle(v) {
return new GC.Spread.Sheets.LineBorder(v.color, v.style);
}
function foreachObj(obj, func) {
if (!obj) return;
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var v = obj[key];
func(key, v);
}
}
<!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$/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 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;
}