Mun*_*key 2 javascript forms alert google-sheets google-apps-script
我有一个我正在尝试开发的电子表格,可以使用它的一些帮助.
表单的快速摘要,是否包含需要人工注意/工作的案例/项目.
在它到达人类之前,任何邮政编码都是正确的非常重要.所以在提供表格的表格上,我有一个正则表达式,确保邮政编码格式正确/正确的字符等.
这并不能防止所有错误,所以在工作表本身,我有一个检查邮政编码的vlookup,如果找不到匹配,则在工作表的其他地方添加了错误的邮政编码(列BN),我们知道该项目需要修复.
这仍然不理想,因为它需要人找到所有"不正确的邮政编码"条目并更正邮政编码.这些可以隐藏/点缀在一张2000多个条目中.
所以我的下一步是创建一个查找"不正确的邮政编码"值的脚本,并记录它所找到的工作表的行.使查找和修复更容易.它还会告诉您总错误数量.
这是代码:
function postcodeFix() {
var sourceSheet = "Form Responses";
var postcodeError = "Incorrect Postcode";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sourceSheet);
var values = sheet.getRange("BN3:BN").getValues();
var errorArray = [];
for (i = 0; i < values.length;i++) {
if (values[i] == postcodeError)
{
errorArray.push(i+3);
}
}
if (errorArray.length <= 0)
{
Browser.msgBox("No Errors Found","All postcodes are correct, no errors found.",Browser.Buttons.OK);
}
else
{
Browser.msgBox("Errors Found","There are currently " + errorArray.length + " Postcode errors that need correcting. " + " The errors are located on the following lines of the 'Form Responses' sheet: "+errorArray+
". Would you like to fix these errors now?", Browser.Buttons.YES_NO);
}
}
Run Code Online (Sandbox Code Playgroud)
完成项目的这一部分后,它让我思考,使用脚本生成的数据.
可能以某种方式提供表格/ ui.这会加载错误的条目,允许您通过表单/ ui而不是电子表格本身更正它们.
我不是Javascript的专家,但我想我可以解决如何管理提示响应,YES.NO等等.不确定如何创建或填充表单/ ui.
有没有人做过类似的事情,或者得到任何方便的链接或指针?
你可以使用一个Modal Dialog盒子.对话框可以包含自定义HTML.
添加此代码:
// This will run when the spreadsheet is opened or the browser page is refreshed
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.createMenu('Custom Menu')
.addItem('Open Dialog Box', 'openDialog')
.addToUi();
}
Run Code Online (Sandbox Code Playgroud)
该onOpen()功能将在电子表格打开时运行.
在.gs脚本文件中,添加此功能.
function openDialog() {
var html = HtmlService.createHtmlOutputFromFile('index')
.setSandboxMode(HtmlService.SandboxMode.NATIVE);
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showModalDialog(html, 'Put a Title Description HERE');
}
Run Code Online (Sandbox Code Playgroud)
在Apps脚本代码编辑器中,
选择:
输入此HTML:
<br>
The NEW Zip: <input type="text" />
<br>
<br>
<input type="button" value="Get The Information" onclick='injectSomeText()'/>
<br>
<br>
<input type="button" value="Get Sheet Data" onclick='getSheetData()'/>
Here is your information!
<div id='myZipInfo'></div>
<br><br>
<input type="button" value="Close"
onclick="google.script.host.close()" />
<script>
window.injectSomeText = function() {
console.log('it ran');
document.getElementById('myZipInfo').textContent = 'This is test text. It worked!';
};
window.onSuccess = function(returnedData) {
document.getElementById('myZipInfo').textContent = returnedData;
}
window.getSheetData = function() {
google.script.run.withSuccessHandler(onSuccess)
.readSheetData();
}
</script>
Run Code Online (Sandbox Code Playgroud)
使用名称"index"保存文件
现在在Code.gs文件中添加一些代码.
function readSheetData() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
for (var i = 0; i < data.length; i++) {
Logger.log('Column One: ' + data[i][0]);
Logger.log('Column Two: ' + data[i][1]);
}
return data;
}
Run Code Online (Sandbox Code Playgroud)
保存Code.gs文件并使用Google电子表格刷新浏览器窗口,该onOpen()函数将运行.
此示例显示了在模式对话框中放置HTML,按钮和输入字段的基础知识.
您可以创建一个名为" 固定邮政编码"的按钮 ,并使其运行您的postcodeFix()功能.
<input type="button" value="Fixed Post Code" onclick="google.script.run.postcodeFix()"/>
Run Code Online (Sandbox Code Playgroud)
您仍然需要配置代码以从电子表格的正确行和列中检索数据,并找出将该信息注入HTML的最佳方法.您可能希望将数据配置为数组或对象或两者的某种组合.
此示例显示可以将HTML注入对话框.单击"获取信息"按钮,文本将出现在对话框中.
请注意,index.html文件的HTML中有一个HTML SCRIPT标记.您实际上可以添加客户端JavaScript并使其在模态对话框中运行.
以下是特定于您的电子表格的解决方案:
function readSheetData() {
var sheet = SpreadsheetApp.getActiveSheet();
var values = sheet.getDataRange().getValues();
var lookup = "Incorrect Postcode";
var data = [];
for (var i = 0; i < values.length; i++) {
Logger.log(values[i][65]);
if (values[i][65] == lookup){
data.push(values[i][3]); //Push Zip Code to Array
data.push(i+1); //Push Row Number to array which is same as iteration number + 1
}
};
Logger.log(data)
return data;
}
Run Code Online (Sandbox Code Playgroud)
<style>
table, td {
border: 1px solid black;
}
</style>
<input type="button" value="Get Bad ZIPs" onclick='getSheetData()'/>
<br>
<br>
<div id='msgInfo' style='display:none'> Here is your information! </div>
<br>
<table id="myTable">
<th>Zip Code</th>
<th>Row Number</th>
<th>New ZIP</th>
</table>
<br>
<br>
<input type="button" value="Close"
onclick="google.script.host.close()" />
<script>
window.onSuccess = function(returnedData) {
document.getElementById('msgInfo').style = 'display:inline';
// Find a <table> element with id="myTable":
var table = document.getElementById("myTable");
console.log(returnedData.length);
for (var i=0; i < returnedData.length; i = i+2) {
console.log("i: " + i);
console.log("returnedData[i]: " + returnedData[i]);
// Create an empty <tr> element and add it to the 1st position of the table:
var row = table.insertRow(i/2);
// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
// Add some text to the new cells:
cell1.innerHTML = returnedData[i];
cell2.innerHTML = returnedData[i+1];
var element1 = document.createElement("input");
element1.type = "text";
element1.name = "txtbox[]";
cell3.appendChild(element1);
};
}
window.getSheetData = function() {
google.script.run.withSuccessHandler(onSuccess)
.readSheetData();
}
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2442 次 |
| 最近记录: |