繁体   English   中英

用 google.script.run 替换 var

[英]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 Script 端),以获得这篇文章的类似结果Share data from .gs (Google Apps Script) to <script> variable in html但是,用“边缘”代替“节点”?

它可以是google.script.run.withSuccessHandler(nodes, edges => {...}).coneArray().edgeArray(); ?

谷歌脚本端

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:目标是将节点与谷歌电子表格中的边数据连接起来。

PS:我分享整个代码是因为,其中一部分没有意义。

  • 在您的情况下,Google Apps Script 中有两个函数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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM