我在js中非常基础,以下代码对我有一点问题。
我如何将var edges = [{from: 1, to: 3}, {from: 1, to: 2}, {from: 2, to: 4}, {from: 2, to: 5}, {from: 3, to: 3}];
(在HTML和Javacript方面)与一种getArray()
(在Google Script方面)相关联,以得到与本帖子Share data from .gs (Google Apps Script) to <script> variable in html类似的结果,但带有“边而不是“节点”?
可以是google.script.run.withSuccessHandler(nodes, edges => {...}).coneArray().edgeArray();
?
Google脚本端
function showBox() {
var template = HtmlService.createTemplateFromFile("Map");
var html = template.evaluate();
html.setHeight(450).setWidth(650);
SpreadsheetApp.getUi().showModelessDialog(html, "Mind Map");
}
function include() {
return HtmlService.createHtmlOutputFromFile().getContent();
}
//-----------------------------------------------------------------------------------Global variables
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
//-----------------------------------------------------------------------------------Nodes function
function coneArray(){
const data = ss.getRange(2,1,ss.getLastRow()-1,5).getValues();
let nodeArray = []
data.forEach(element => {
let obj = {}
obj.id = element[4]
obj.label = element[0]
obj.group = element[1]
obj.x = element[2]
obj.y = element[3]
nodeArray.push(obj)
return element
}
)
Logger.log(nodeArray);
return nodeArray;
}
//-----------------------------------------------------------------------------------Edges function
function lineArray(){
const data = ss.getRange(2,5,ss.getLastRow()-1,2).getValues();
let edgArray = []
data.forEach(element => {
let obj = {}
obj.to = element[1]
obj.from = element[0]
edgArray.push(obj)
return element
}
)
Logger.log(edgArray);
return edgArray;
}
HTML和Javacript端
<!doctype html>
<html>
<head>
<title>Network</title>
<script type="text/javascript" src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script>
<style type="text/css">
#mynetwork {
width: 600px;
height: 400px;
border: 1px solid lightgray;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<input type="button" value="Reload" onclick="google.script.run.showBox()" />
<div id="mynetwork"></div>
<script type="text/javascript">
var container = document.getElementById('mynetwork');
var options = {edges: {smooth: false}, physics: {enabled: false}, nodes: {shape: "square",size: 10}};
var edges = [{from: 1, to: 3}, {from: 1, to: 2}, {from: 2, to: 4}, {from: 2, to: 5}, {from: 3, to: 3}];
google.script.run.withSuccessHandler(nodes => {
var data = {nodes: nodes, edges: edges};
var network = new vis.Network(container, data, options);
}).coneArray();
</script>
</body>
</html>
在此之前,非常感谢!
PS:目标是将节点与Google电子表格中的边缘数据连接起来。
PS:我共享整个代码,因为在某种程度上没有意义。
coneArray()
和edgeArray()
两个功能。您想从两个函数中检索值,并使用这些值,并想使用vis.js。我可以像上面那样理解。如果我的理解是正确的,那么下面的修改如何?
在此修改中,同时检索了coneArray()
和edgeArray()
的值,并且所检索的值用于vis.js。
[请将以下脚本添加到Google Apps脚本。
const sample = () => ([coneArray(), edgeArray()]);
请如下将google.script.run
的脚本替换为。
google.script.run.withSuccessHandler(([nodes, edges]) => new vis.Network(container, {nodes: nodes, edges: edges}, options)).sample();
如果要从Java脚本调用每个函数,也可以将以下Java脚本用作简单脚本。但是我想推荐上面修改过的脚本。
google.script.run.withSuccessHandler(nodes => {
google.script.run.withSuccessHandler(edges => {
new vis.Network(container, {nodes: nodes, edges: edges}, options)
}).edgeArray();
}).coneArray();