使用脚本中的数据,通过各种形式或提醒框修改 Google 电子表格中的数据

Edit data in a Google Spreadsheet, via a form of sorts or alert box, using data from a script

本文关键字:数据 修改 电子表格 Google 脚本      更新时间:2023-09-26
我有一个电子表格,我

正在尝试开发它,可以使用一些帮助。

工作表的快速摘要是,它包含需要人工注意/工作的案例/项目。

在它到达人类之前,任何邮政编码都是正确的是非常重要的。因此,在输入工作表的表单上,我有一个正则表达式,可确保邮政编码采用正确的格式/正确的字符等。

这并不能防止所有错误,因此在工作表本身中,我有一个检查邮政编码的 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);
}


 }

完成项目的这一部分后,它让我思考,使用脚本中的结果数据。

可以以某种方式提供表单/UI吗?这会加载不正确的条目,允许您通过表单/UI 而不是电子表格本身更正它们。

我不是Javascript的专家,但我认为我可以研究如何管理快速响应,YES.NO 等。只是不确定如何创建或填充表单/ui。

以前有没有人做过类似的事情,或者有任何方便的链接或指针?

您可以使用

Modal Dialog框。 对话框可以包含自定义 HTML。

创建一个 onOpen() 函数

  • 单击"工具"菜单
  • 单击脚本编辑器

添加此代码:

// 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();
}

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');
}

创建索引.html文件

在应用程序脚本代码编辑器中,

选择:

  • 文件
  • 新增功能
  • 网页文件

输入此 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>

使用名称"索引"保存文件

现在向 Code.gs 文件添加更多代码。

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;
}

保存 Code.gs 文件并使用 Google 电子表格刷新浏览器窗口,onOpen() 函数将运行。

此示例演示在模式对话框中放置 HTML、按钮和输入字段的基础知识。

您可以创建一个名为:固定邮政编码的按钮,并使其运行您的postcodeFix()功能。

<input type="button" value="Fixed Post Code" onclick="google.script.run.postcodeFix()"/>

您仍然需要配置代码以从电子表格的正确行和列中检索数据,并找出将该信息注入 HTML 的最佳方式。 您可能希望将数据配置为数组或对象或两者的某种组合。

此示例显示可以将 HTML 注入到对话框中。 单击"获取信息"按钮,文本将出现在对话框中。

请注意,索引.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;
}

新网页

<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>