简体   繁体   English

如何在 Google Apps 脚本中查找和替换值?

[英]How to Find & Replace Value in Google Apps Script?

I'm building a simple data entry form in Google Sheets.我正在 Google 表格中构建一个简单的数据输入表单。 The user completes specific fields (in Sheet A) and then clicks the Submit button.用户填写特定字段(在工作表 A 中),然后单击提交按钮。 The data is then entered into another sheet (Sheet B).然后将数据输入到另一个工作表(工作表 B)中。

I'm looking for a scripting solution that will take the value from the data entry form (Sheet A), do a find and replace (In the script), then place the replaced value in Sheet B. Once the data is entered into Sheet B, the values in Sheet A will be nulled automatically.我正在寻找一个脚本解决方案,它将从数据输入表单(表 A)中获取值,执行查找和替换(在脚本中),然后将替换的值放在表 B 中。一旦将数据输入到表中B,表 A 中的值将自动清零。

Examples:例子:

  • User entered value is PASS.用户输入的值为 PASS。 Value entered on data form is 1.在数据表单上输入的值为 1。
  • User entered value is FAIL.用户输入的值为 FAIL。 Value entered on data form is 0.在数据表单上输入的值为 0。

Link to Google Sheet: https://docs.google.com/spreadsheets/d/10c1EX27WbINQOjN0UyE778gl8LguKp7MRKwQdaw0mu4/edit?usp=sharing谷歌表格链接: https : //docs.google.com/spreadsheets/d/10c1EX27WbINQOjN0UyE778gl8LguKp7MRKwQdaw0mu4/edit?usp=sharing

Script:脚本:

function submitData() {
  var ss        = SpreadsheetApp.getActiveSpreadsheet();
  var formSS    = ss.getSheetByName("Form"); //Form Sheet
  var datasheet = ss.getSheetByName("Form Data"); //Data Sheet
  
  //Input Values
  var values = [[formSS.getRange("L5").getValue(),
                 formSS.getRange("D18").getValue(),
                 formSS.getRange("D21").getValue(),
                 formSS.getRange("R18").getValue(),
                 formSS.getRange("R21").getValue(),
                 formSS.getRange("AF18").getValue(),
                 formSS.getRange("AF21").getValue(),
                 formSS.getRange("D33").getValue(),
                 formSS.getRange("D36").getValue(),
                 formSS.getRange("R33").getValue(),
                 formSS.getRange("R36").getValue(),
                 formSS.getRange("AF33").getValue(),
                 formSS.getRange("AF36").getValue(),
                 formSS.getRange("D48").getValue(),
                 formSS.getRange("D51").getValue(),
                 formSS.getRange("R48").getValue(),
                 formSS.getRange("R51").getValue(),
                 formSS.getRange("AF48").getValue(),
                 formSS.getRange("AF51").getValue(),
                 formSS.getRange("D63").getValue(),
                 formSS.getRange("D66").getValue(),
                 formSS.getRange("R63").getValue(),
                 formSS.getRange("R66").getValue(),
                 formSS.getRange("AF63").getValue(),
                 formSS.getRange("AF66").getValue()]];
  
  datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 25).setValues(values);
}

Can anyone help?任何人都可以帮忙吗?

Explanation:解释:

User entered value is PASS.用户输入的值为 PASS。 Value entered on data form is 1.在数据表单上输入的值为 1。

User entered value is FAIL.用户输入的值为 FAIL。 Value entered on data form is 0.在数据表单上输入的值为 0。

Since you already have the values, you can achieve the above transformation using the following ternary operator:由于您已经有了这些值,您可以使用以下三元运算符实现上述转换:

var f_values = values.map(val=>val=='Pass'?1:val=='Fail'?0:val);

The latter will iterate over the values array and it will transform every value to: 1 if val=='Pass' , 0 if val=='Fail' or keep as is if the other two cases evaluate to false.后者将遍历values阵列,它将每一个值变换到:1 if val=='Pass' ,0 if val=='Fail'或保持如如果其它两种情况评估为假。


Solution:解决方案:

This is what you are looking for:这就是你要找的:

function submitData() {
  var ss        = SpreadsheetApp.getActiveSpreadsheet();
  var formSS    = ss.getSheetByName("Form"); //Form Sheet
  var datasheet = ss.getSheetByName("Form Data"); //Data Sheet
  
  var values = [formSS.getRange("L5").getValue(),
                 formSS.getRange("D18").getValue(),
                 formSS.getRange("D21").getValue(),
                 formSS.getRange("R18").getValue(),
                 formSS.getRange("R21").getValue(),
                 formSS.getRange("AF18").getValue(),
                 formSS.getRange("AF21").getValue(),
                 formSS.getRange("D33").getValue(),
                 formSS.getRange("D36").getValue(),
                 formSS.getRange("R33").getValue(),
                 formSS.getRange("R36").getValue(),
                 formSS.getRange("AF33").getValue(),
                 formSS.getRange("AF36").getValue(),
                 formSS.getRange("D48").getValue(),
                 formSS.getRange("D51").getValue(),
                 formSS.getRange("R48").getValue(),
                 formSS.getRange("R51").getValue(),
                 formSS.getRange("AF48").getValue(),
                 formSS.getRange("AF51").getValue(),
                 formSS.getRange("D63").getValue(),
                 formSS.getRange("D66").getValue(),
                 formSS.getRange("R63").getValue(),
                 formSS.getRange("R66").getValue(),
                 formSS.getRange("AF63").getValue(),
                 formSS.getRange("AF66").getValue()];
  
  var f_values = values.map(val=>val=='Pass'?1:val=='Fail'?0:val);
  
  datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 25).setValues([f_values]);
}

References:参考:

Array.prototype.map() Array.prototype.map()

Conditional (ternary) operator 条件(三元)运算符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Google Apps 脚本中修复或调整此查找和替换 - How to fix or adapt this Find and Replace in Google Apps Script 替换 Google Script Apps 中的单元格值 - Replace cell value in Google Script Apps Google Apps脚本中的“ .Replace” - “.Replace” in Google Apps Script 在附加到Google文档的Google Apps脚本中通用查找和替换 - Universal find and replace within google apps script attached to google docs 用于在 Google 表格中进行多次查找和替换的 Google Apps 脚本 - Google Apps Script for Multiple Find and Replace in Google Sheets Google Apps脚本多个在Google表格中查找和替换正则表达式 - Google Apps Script Multiple Find and replace regex in Google Sheets Google Apps 脚本或 JavaScript - 查找和替换结果比要求的高一排 - 如何将其上移一排以进行替换? - Google Apps Script or JavaScript - Find & Replace result is one row higher than required - How do I move it one row up to replace? 如何在 Google Apps 脚本中获取复选框的值 - how to get value of checkbox in Google Apps script Google Apps脚本:查找具有特定值的单元格,然后设置其背景色 - Google Apps Script: Find Cells With a Certain Value, Then Set the Background Color of It 如何使用 Google Apps 脚本替换电子表格中的文本? - How do I replace text in a spreadsheet with Google Apps Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM