将数据从.gs(Google Apps脚本)共享到< script>HTML中的变量 [英] Share data from .gs (Google Apps Script) to <script> variable in html

查看:55
本文介绍了将数据从.gs(Google Apps脚本)共享到< script>HTML中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[
  {"id":1,"label":"Node 2"},
  {"id":2,"label":"Node 3"},
  {"id":3,"label":"Node 4"},
  {"id":4,"label":"Node 5"}
]

嗨!在以下代码中,函数 getArray()返回此字符串↑.您是否知道如何将其与.html中的变量 nodes 连接.我粘贴了代码,谢谢!

Hi! In the following code, the function getArray() returns this string ↑. Do you know how to connect it with the variable nodes in the .html . I pasted the codes, Thanks!

function getArray(){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const data = ss.getRange(1,1,ss.getLastRow()-1,2).getValues();

  let objArray = []
  data.forEach(element => {
    let obj = {}
    obj.id = element[0]
    obj.label = element[1]
    objArray.push(obj)
    return element
    }
  )
var obj = JSON.stringify(objArray);
//Logger.log(obj);
return obj;
}

<!doctype html>
<html>
<head>
  <title>Network</title>
</head>
<body>
<div id="mynetwork"></div>
<script type="text/javascript">

var nodes = [
  {"id":1,"label":"Node 2"},
  {"id":2,"label":"Node 3"},
  {"id":3,"label":"Node 4"},
  {"id":4,"label":"Node 5"}
];

</script>
</body>
</html>

回顾:我的目标是将.gs中返回的"obj"用作.html中的var"nodes".我不知道编程演讲是否正确.谢谢!

Recap: My goal is use the returned "obj" in .gs as var "nodes" in .html. I don't know if the programming speech its correct. Thanks!

如果有些人可以分享我的代码,我将非常感谢您!

If some can share me the code, im going to thanks you a lot thanks!

推荐答案

此修改如何?请认为这只是几个可能的答案之一.

How about this modification? Please think of this as just one of several possible answers.

  • 在这个答案中, nodes 的值由 google.script.run 检索.
  • 在GAS端的 getArray()处,该值作为对象返回.
  • 在Javascript端, nodes 的对象用于vis.js.
  • In this answer, the value of nodes are retrieved by google.script.run.
  • At getArray() in GAS side, the value is returned as an object.
  • At Javascript side, the object of nodes is used for vis.js.
function showBox() {
   var template = HtmlService.createTemplateFromFile("Map");
   var html = template.evaluate();
   html.setHeight(450).setWidth(650);
   SpreadsheetApp.getUi().showModalDialog(html, "New Title");
}

function getArray(){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const data = ss.getRange(1,1,ss.getLastRow()-1,2).getValues();

  let objArray = []
  data.forEach(element => {
    let obj = {}
    obj.id = element[0]
    obj.label = element[1]
    objArray.push(obj)
    return element
    }
  )
  return objArray;  // Modified
}

HTML和Javascript方面:

<!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>
<div id="text"></div>
<div id="mynetwork"></div>
<script type="text/javascript">

// Modified
google.script.run.withSuccessHandler(nodes => {
  var edges = [{from: 1, to: 3}, {from: 1, to: 2}, {from: 2, to: 4}, {from: 2, to: 5}, {from: 3, to: 3}];
  var container = document.getElementById('mynetwork');
  var data = {nodes: nodes, edges: edges};
  var options = {edges: {smooth: false}, physics: {enabled: false}};
  var network = new vis.Network(container, data, options);
}).getArray();

</script>
</body>
</html>

参考:

  • google.script.run类
  • 这篇关于将数据从.gs(Google Apps脚本)共享到&lt; script&gt;HTML中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆