简体   繁体   English

如何更新 HTML 中通过 Google 脚本从 Google 表格中提取变量的文本

[英]How can I update text in HTML that pulls a variable from Google Sheets through a Google Script

I've tried various methods and confirmed each function works, but I can't get Google Scripts and the HTML to talk to each other.我尝试了各种方法并确认每个 function 都有效,但我无法让 Google Scripts 和 HTML 相互交谈。 My.gs file looks for the accountid from a url parameter (similar to a vlookup) and then spits out the value for the columns that are identified by the lookup value. My.gs 文件从 url 参数(类似于 vlookup)中查找 accountid,然后吐出由查找值标识的列的值。 If I cannot find the accountid, I leave a value of 10 as default for both "credits remaining" and "credits total".如果我找不到 accountid,我会将“剩余学分”和“学分总数”的值都保留为默认值 10。 Once the.gs file processes, I want to have that automatically pull into my HTML file to mark the creditsremaining and credtistotal variables.处理 .gs 文件后,我想让它自动拉入我的 HTML 文件以标记 creditsremaining 和 credtistotal 变量。 This will in turn populate the text and make the bar graph calculate a width.这将依次填充文本并使条形图计算宽度。

Any thoughts on how to pass over the variables correctly?关于如何正确传递变量的任何想法?

.gs .gs

function credsearch(searchid) {
   try {
  //  var searchid = "456"
  var tosearch = searchid
   var s = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();     

 var data = SpreadsheetApp.openById("1Okxy3sUkPrKy1wBRs5yw8AAv4qHH3ldOfsA3usziVDo").getSheetByName("Sheet1").getRange('A2:Z').getValues()
 var spreadsheet = SpreadsheetApp.getActive();
  var tf = spreadsheet.createTextFinder(tosearch);
  var all = tf.findAll();
  for (var i = 0; i < all.length; i++) {
    var searchvalue = ('%s', all[i].getA1Notation());
  }
 
 var searchValue = s.getRange(searchvalue).getValue();
 var dataList = data.map(x => x[0])
 var index = dataList.indexOf(searchValue);

 if (index === -1) {

    var creditsremaining = "10"
    var creditstotal = "10"

    console.log(creditsremaining)
    console.log(creditstotal)
    return { creditsremaining: creditsremaining, creditstotal: creditstotal };

 } else {

    var creditsremaining = data[index][1]
    var creditstotal = data[index][2]

    console.log(creditsremaining)
    console.log(creditstotal)
    return { creditsremaining: creditsremaining, creditstotal: creditstotal };
 }
}catch(e) {
  alert(e)
 }
 return { creditsremaining: "Null", creditstotal: "Null" };
}

.HTML .HTML

<style>
.grid .bar {
    background-color: #301c4a;
    margin-bottom: 10px;
    align-self: flex-end;
    border-radius: 10px;
    position: relative;
    text-align: left;
}
#speciallicenses .grid.horizontal {
    flex-direction: column;
    border-bottom: none;
    background: repeating-linear-gradient(90deg,transparent,transparent 19.5%,rgba(0,0,0,0) 20%);
}
      
.grid.horizontal {
    flex-direction: column;
    border-bottom: none;
    background: repeating-linear-gradient(90deg,transparent,transparent 19.5%,rgba(0,0,0,0) 20%);
    border-radius: 10px;
}
.grid.horizontal .bar {
    height: 45px;
    width: var(--bar-value);
    align-self: flex-start;
    margin: auto 0 auto 0;
    border-radius: 10px;
}
.grid.horizontal .bar::after {
    top: initial;
    left: 100%;
    padding: 0 10px;
    display: inline-block;
    white-space: nowrap;
    position: absolute;
    transform: rotate(0deg);
    line-height: 45px;
}
</style>
<div class="grid horizontal" style="background-color:#eeeeee"> 
<div id="creditsbar" class="bar" style="vertical-align:center;width:50%;margin-bottom:0px;padding-top:10px;padding-left:10px;padding-bottom:0px;color:#ffffff"> 
<span id="creditsremaining"></span>/<span id="creditstotal"></span>
</div> </div>


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
google.script.url.getLocation(function(location) {
         accountid = location.parameters["accountid"]
         console.log(location.parameters);
         console.log(location.hash);
        });
 $(document).ready(function () {
function searchforcredits() {
var searchid = accountid
google.script.run.withSuccessHandler(updateCredits).credsearch(searchid);
}
function updateCredits(result) {
var creditsremaining = result.creditsremaining;
var creditstotal = result.creditstotal;

}  
 
  $('span#creditsremaining').text(creditsremaining);
  $('span#creditstotal').text(creditstotal);
  var barwidth = (creditsremaining/creditstotal) *100;
  var barpercent = barwidth + '%';
   $('#creditsbar').css("width", barpercent);
});
</script>

You could simply do this.你可以简单地这样做。

$(document).ready(function () {
  google.script.run.withSuccessHandler(updateCredits).credsearch(accountid);
}

暂无
暂无

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

相关问题 使用 Google Apps 脚本,如何替换 Google 表格模板中的文本以制作新表格? - Using Google Apps Script, how can I replace text in a Google Sheets template to make a new Sheet? 如何将变量从HTML脚本文件传递到Google Apps脚本中的javascript函数? - How can I pass a variable from an HTML script file to a javascript function in google apps script? 使用 Google 表格中的数组在 html 中创建一个数据列表,该列表可提供给 Google Apps 脚本以自动完成文本输入 - use an array from Google Sheets to create a data list in html that can be served to a Google Apps Script for Autocomplete a Text Input 我该如何编写Google表格脚本来对特定列进行排序,并删除所有不在特定字符串数组中的内容 - How can I write a google sheets script that will sort through a particular column and delete everything that is not in a specific array of strings 运行仪表应该跟踪的另一个功能后,如何在 Google 表格项目中的 Google Apps 脚本中更新进度表 - How can I update a progress meter in Google Apps script in a Google Sheets project after running another function that the meter is supposed to track 如何使用谷歌表格中的脚本计算这些单词? - How can I count these words, using script in google sheets? 我怎样才能使这个谷歌表脚本代码更短/更快? - How can I make this google sheets script code shorter/faster? 如何将工作表列表添加到 Google App Script? - How can i add list of sheets to Google App Script? 如何优化此脚本以在 Google 表格中更好地工作? - How can I optimize this script to work better in google sheets? 如何从表格中计算谷歌应用脚​​本中的持续时间? - How do I calculate duration in google apps script from sheets?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM