html - 如何在自定义 HTML 对话框中获取日期文本输入以加载来自 Google 表格的值?
问题描述
我正在尝试将 Google 表格中自定义 (HTML) 对话框上的文本输入值更改为等于我的 Google 表格中的值。我怎样才能做到这一点?
我知道为了设置文本输入的 .value 需要应用正确的日期格式。我可以使用 .gas 中的“实用程序”服务。
我尝试使用该onload="getDate()"
事件来运行一个函数,该函数使用var date = new Date()
in设置值,getDate()
而不是直接从工作表中获取值,但这似乎不起作用。
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
label {font-family: verdana;}
body {
border: 1px solid powderblue;
padding: 30px;
}
</style>
</head>
<body onload="getDate()">
<form id="myForm">
<label>Task Number:</label><br>
<input type="text" name="taskNumber" value="">
<br><br>
<label>Task Date:</label><br>
<input type="date" name="taskDate" value="" id="demo" >
<br><br>
<label>Customer Name:</label><br>
<input type="text" name="customerName" value="">
<br><br>
<label>Customer Site:</label><br>
<input type="text" name="customerSite" value="">
<br><br>
<label>Status:</label><br>
<select name="status">
<option value="NOSTATUS">Choose</option>
<option value="ON HOLD">ON HOLD</option>
<option value="SCHEDULED">SCHEDULED</option>
<option value="RESCHEDULE">RESCHEDULE</option>
<option value="WORK IN PROGRESS">WORK IN PROGRESS</option>
</select>
<br><br>
<label>Status Date:</label><br>
<input type="date" name="statusDate" value="">
<br><br>
<label>Location:</label><br>
<input type="text" name="location" value="">
<br><br>
<label>Description:</label><br>
<input type="text" name="description" value="">
<br><br>
<input type="button" style="font-family: verdana;" value="Submit" onclick="uploadTask()">
</form>
<script>
function success(msg) {
alert(msg);
}
function uploadTask(){
var form = document.getElementById("myForm").elements;
var obj ={};
for(var i = 0 ; i < form.length ; i++){
var item = form.item(i);
obj[item.name] = item.value;
}
google.script.run
.withSuccessHandler(success)
.uploadTask(obj);
google.script.host.close();
}
function getDate(){
var ss = SpreadsheetApp.openById('1CtH3uNeSiJcRd_ZKE3iWp7TEtAO_B7uCNatGJ7rHMeg');
var monthly = ss.getSheetByName('MONTHLY');
var date = monthly.getRange(1, 1).getValue(); //The value of A:1 = 10/12/2000 date
date = Utilities.formatDate(date, Session.getScriptTimeZone(), "YYYY-MM-dd")
document.getElementById("demo").value = date;
}
</script>
</body>
</html>
解决方案
这个改装怎么样?我认为您的情况有几个答案。因此,请将此视为其中之一。
修改点:
- 在您的显示脚本中,HTML 中有 GAS 脚本。GAS 不能直接在 HTML 中运行。
- 在这种情况下,我使用了
google.script.run
. - 中的 GAS 脚本
getDate()
已从“.html”移至“.gs”。
- 在这种情况下,我使用了
修改后的脚本:
请getDate()
在“.html”文件中修改为以下脚本。
function getDate() {
google.script.run.withSuccessHandler((e) => {
document.getElementById("demo").value = e;
}).getDate();
}
接下来,请将以下函数添加到“.gs”文件中。
function getDate() {
var ss = SpreadsheetApp.openById('1CtH3uNeSiJcRd_ZKE3iWp7TEtAO_B7uCNatGJ7rHMeg');
var monthly = ss.getSheetByName('MONTHLY');
var date = monthly.getRange(1, 1).getValue(); //The value of A:1 = 10/12/2000 date
return Utilities.formatDate(date, Session.getScriptTimeZone(), "YYYY-MM-dd");
}
笔记:
- “.gs”文件是您使用的项目中的脚本之一。我认为它是用来打开对话框的。
- 如果“.gs”文件中有相同的函数名,请修改。
参考:
推荐阅读
- java - 与 Java 内部私有类和 toString() 混淆
- c# - 如何使用 Consul 一次在一台机器上执行同步任务?
- javascript - 通过post请求nodejs提交表单后重定向到其他url
- assembly - 为什么 2 的补码扩展通过添加符号位的副本来工作?
- node.js - 如何使用包含前端和后端的 web 包运行反应项目
- r - 使用来自另一个 DF 的值创建 Dataframes 列并在 R 中匹配更新
- python - 如何使用numpy查找最大的先前x元素
- reactjs - require('devtron').install() 在电子应用程序中导致“Uncaught TypeError: Cannot read property 'BrowserWindow' of undefined”
- postgresql - 通过表达式或正则表达式的方式在sql表达式中查找特定值
- java - 在 toString() 方法之后使用 equals() 方法的问题