将 var 替换为 google.script.run

Replace a var with google.script.run

我的 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 脚本端)联系起来,以获得post 的类似结果,但是用“边”而不是“节点”?

可以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: The goal is connect the nodes with the edges data in a google spreadsheets.

PS: I share the entire code because, will not have sense in a part.

  • 根据您的情况,Google Apps 脚本中有 coneArray()edgeArray() 两个函数。你想从两个函数中检索值并使用这些值,你想使用 vis.js.

我可以像上面那样理解。如果我的理解是正确的,下面的修改怎么样?

在此修改中,检索值 coneArray()edgeArray(),检索值用于 vis.js。

Google Apps 脚本端:

请将以下脚本添加到 Google Apps 脚本中。

const sample = () => ([coneArray(), edgeArray()]);

HTML和Javascript方:

请将您的 google.script.run 脚本替换为如下内容。

google.script.run.withSuccessHandler(([nodes, edges]) => new vis.Network(container, {nodes: nodes, edges: edges}, options)).sample();

注:

  • 如果要从Javascript调用各个函数,也可以使用下面的Javascript作为一个简单的脚本。但我想推荐上面修改过的脚本。

    google.script.run.withSuccessHandler(nodes => {
      google.script.run.withSuccessHandler(edges => {
        new vis.Network(container, {nodes: nodes, edges: edges}, options)
      }).edgeArray();
    }).coneArray();