如何从 Google Sheets 应用程序脚本正确部署 Web 应用程序?

问题描述 投票:0回答:1

我正在尝试为我的员工制作一个在线网络应用程序,以帮助他们完成除草剂的混合和加载过程。随着外部温度升高,需要稀释,因此目标是让他们输入温度和罐中当前的体积,然后返回需要添加的量。我对部署非常陌生,我的 doGet() 函数似乎可以正常工作,但结果是一个空白的白页。 这是代码:

var high_max_temp = 22; //Celsius
var mid_max_temp = 28; //Celsius
var tankSize = 200; //Gallons
var maxFiesta = 30; //Liters
var maxActivate = 10; //Liters
var fiestaRatio = tankSize/maxFiesta; //30L for a full tank, full strength
var activateRatio = tankSize/maxActivate; //10L for a full tank, full strength
var midStrengthMultiplier = 0.75; //75% Strength
var lowStrengthMultiplier = 0.3; //30% Strength

function doGet(e) {
  var result = '';
  if (e.parameter.temperature && e.parameter.volume) {
    var temperature = parseFloat(e.parameter.temperature);
    var volume = parseFloat(e.parameter.volume);
    Logger.log('Temperature: ' + temperature + ', Volume: ' + volume);
    var fiestaValue = fiestaDilutionCalculator(volume, temperature);
    Logger.log('Fiesta Value: ' + fiestaValue);
    result = '<h1>Result:</h1><p>Fiesta Dilution Value: ' + fiestaValue.toFixed(2) + '</p>';
  } else {
    result = '<h1>Fiesta Dilution Calculator</h1>' +
             '<form>' +
             '  Temperature (°C): <input type="text" name="temperature"><br>' +
             '  Volume (mL): <input type="text" name="volume"><br>' +
             '  <input type="submit" value="Calculate">' +
             '</form>';
  }
  return HtmlService.createHtmlOutput(result);
}

//Calculates the required fiesta depending on tank volume and temperature
function fiestaDilutionCalculator(volume,temp) {
  
  //get strength based on temperature
  var strength = 1;
  if (temp <= high_max_temp){
    strength = 1;
  }
  else if(temp > high_max_temp && temp <= mid_max_temp){
    strength = midStrengthMultiplier;
  }
  else if(temp > mid_max_temp){
    strength = lowStrengthMultiplier;
  }
  
  var fiestaValue = ((tankSize-volume)/tankSize)*(maxFiesta*strength);

  Logger.log(fiestaValue);
  return fiestaValue;
}

在顶部,我有我的变量,后面跟着我的 doGet() 函数。下面是执行计算并产生正确值的函数。当我点击提交按钮时,屏幕为白色并且没有出现“结果”。我在后端缺少什么东西还是我的代码不正确?

提前致谢!

google-apps-script deployment web-applications
1个回答
0
投票

请阅读HTML 服务:与服务器功能通信。本指南包括使用 Google Apps 脚本创建网络应用程序的示例,该脚本提供 HTML 表单、调用服务器端函数并显示该函数的响应。

请注意

e.preventDefault()
的使用。此语句可防止 Web 浏览器在表单提交完成后重定向。

© www.soinside.com 2019 - 2024. All rights reserved.