簡體   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