使用脚本中的数据,通过排序或警报框的形式编辑 Google 电子表格中的数据

2023-12-19

我正在尝试开发一个电子表格,需要一些帮助。

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

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

这并不能防止所有错误,因此在工作表本身中,我有一个 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。这会加载不正确的条目,允许您通过表单/用户界面而不是在电子表格本身上更正它们。

我不是 Javascript 专家,但我认为我可以弄清楚如何管理提示响应、YES.NO 等。只是不确定如何创建或填充表单/用户界面。

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


您可以使用Modal Dialog盒子。该对话框可以包含自定义 HTML。

创建一个 onOpen() 函数

  • 点击TOOLS menu
  • Click 脚本编辑器

添加此代码:

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

That onOpen()函数将在电子表格打开时运行。

创建一个在选择菜单项时运行的函数

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

创建一个index.html文件

在 Apps 脚本代码编辑器中,

Choose:

  • File
  • New
  • 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>

以“index”名称保存文件

现在添加一些更多的代码Code.gs file.

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 注入到对话框中。单击“获取信息”按钮,对话框中将出现文本。

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

New HTML

<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用脚本中的数据,通过排序或警报框的形式编辑 Google 电子表格中的数据 的相关文章

随机推荐