简体   繁体   English

Google 表格脚本:如果 substring 在另一列中,则返回值

[英]Google Sheets script: return value if substring is within another column

I'm new (read: literally just looking at JS for the first time now) to Javascript/Sheets/scripts in general, so forgive me for how basic this is.一般来说,我是 Javascript/Sheets/scripts 的新手(阅读:现在只是第一次看 JS),所以请原谅我这是多么基本。 I've done a lot of independent research with no luck.我做了很多独立的研究,但没有运气。

I have a spreadsheet with a column for full name (all caps - LAST, FIRST format), split into two additional columns of LAST and FIRST (full name split via comma), and then a lower caps column that contains the full name of the person in First Last format.我有一个电子表格,其中有一列全名(全部大写 - LAST、FIRST 格式),分成两列 LAST 和 FIRST(全名通过逗号分隔),然后是一个包含全名的小写列First Last 格式的人。

There are some errors in the data - for instance, LAST, FIRST might be followed by First Last that is a totally different name, or an incorrect spelling of the prior column.数据中存在一些错误 - 例如,LAST、FIRST 后面可能跟有完全不同的名称的 First Last,或者前一列的拼写错误。 I'm basically trying to flag it when there is a mismatch in the contents, either spelling-wise or the name overall.我基本上是在内容不匹配时尝试标记它,无论是拼写还是整体名称。

I've written this simple script that pulls the first name (column D) and the full name in First Last format (column F) and checks whether the first name is contained in the full name.我编写了这个简单的脚本,它以 First Last 格式(F 列)提取名字(D 列)和全名,并检查全名中是否包含名字。 Supposedly, it should return the value of search() in column G. That way I can use conditional formatting to flag any #VALUE, that appears from a failed search.假设它应该在 G 列中返回 search() 的值。这样我可以使用条件格式来标记任何从失败的搜索中出现的 #VALUE。 indicating that the row needs to be checked.表示需要检查该行。

Unfortunately the script runs infinitely and I can't figure out why I'm unable to hit a break point and actually return values in the corresponding columns in the actual sheet.不幸的是,脚本无限运行,我无法弄清楚为什么我无法打断点并实际返回实际工作表中相应列中的值。 This is a tiny project and I am struggling to justify learning the interface between scripts and Sheets from scratch, so I would really appreciate some help!这是一个很小的项目,我正在努力证明从头开始学习脚本和表格之间的界面是合理的,所以我非常感谢一些帮助! Thank you!谢谢!

   function checkName() {
      var s = SpreadsheetApp.getActiveSheet();
      var data = s.getDataRange().getValues();
      var data_len = data.length;
      for(var i=6; i<data_len; i++) {
        var fname = SpreadsheetApp.getActiveSheet().getRange(i, 4).getValue();
        var fullname = SpreadsheetApp.getActiveSheet().getRange(i, 6).getValue();
        s.getRange(i, 7).setValue(fullname.includes(fname));    
      }
    }

Here's how I'd write a function to meet your requirements:以下是我如何编写 function 以满足您的要求:

function checkName() {

  // these two will be used in an Array,
  // which is 0-indexed
  const FIRST_NAME_COLUMN = 3; 
  const FULL_NAME_COLUMN = 5;

  // these two will be used in a Sheets range,
  // which is 1-indexed
  const INCLUDES_COLUMN = 7;
  const FIRST_ROW = 6;

  const s = SpreadsheetApp.getActiveSheet();
  const sheetDataRange = s.getDataRange(); // this is just the whole shebang

  // get the dimensions of the main range
  const RANGE_HEIGHT = sheetDataRange.getHeight();
  const RANGE_WIDTH = sheetDataRange.getWidth();
  
  // use your first row number to constrain the actual data range
  // we'll use this reference to create an array we can easily iterate over
  const dataRange = s.getRange( FIRST_ROW, 1, RANGE_HEIGHT, RANGE_WIDTH );

  // data is a 2-dimensional Array,
  // so you could get values like data[row][column]
  // Also, Arrays are 0-indexed, so every number is 1 less than you had originally
  const data = dataRange.getValues(); 

  Logger.log( data ); // just to inspect

  const includesArray = [];

  for (const row of data) {
    // using a for ... of loop means we don't have to track the index,
    // and we can treat each row as a 1-D array inside the loop
    const firstName = row[FIRST_NAME_COLUMN];
    const fullName = row[FULL_NAME_COLUMN];
    const included = fullName.includes(firstName); // btw this will return a value of TRUE for ones that do include and FALSE for those that don't

    Logger.log( `${firstName}, ${fullName}, ${included}` );
    // debugger;
    includesArray.push( [included] ); // adding an Array that includes just the single element, since that represents a single data column
  }

  Logger.log( includesArray );
  debugger;

  s.getRange( FIRST_ROW, INCLUDES_COLUMN, RANGE_HEIGHT, 1 ).setValues( includesArray );

}

I'm using const when possible, for both scoping and to indicate that I won't be re-assigning the variables.我尽可能使用const来确定范围并表明我不会重新分配变量。 I'm also assigning magic numbers to THESE_CONSTANTS so they have some semantic meaning in the code (that's just for my sanity).我还为 THESE_CONSTANTS 分配了幻数,因此它们在代码中具有一些语义含义(这只是为了我的理智)。

Also, I'm not touching the "database" inside the loop at all - I'm strictly working with JS Arrays here.此外,我根本没有触及循环内的“数据库”——我在这里严格使用 JS Arrays。 I've pulled the data from the Sheet with dataRange.getValues() , then I'm writing all the includes values to the Sheet at once after the loop with setValues() .我已经使用dataRange.getValues()从工作表中提取数据,然后在使用setValues()循环之后立即将所有包含值写入工作表。 This is where you're likely to see the best improvement in terms of performance.这是您可能会在性能方面看到最佳改进的地方。

Give it a shot, and ask any questions you need!试一试,并提出您需要的任何问题!

暂无
暂无

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

相关问题 选择当前行中的列(Google表格脚本) - Select column within current row (Google Sheets Script) 在Google表格脚本编辑器中从json返回值 - Return a value from json in Google Sheets script editor 如何根据同一工作表中列的特定值在 Google 表格的另一列中设置值 - How to set value in another column of Google Sheets based on certain value of column in the same sheets Google表格脚本根据列单元格中的值锁定或解锁行 - Google Sheets Script to lock or unlock a row depending on the value in the cell of a column 如何在 Google 表格上将值从一列复制到另一列? - How to copy value from one column to another on Google Sheets? Google App Script:根据列中的值将行(仅行中选定的列)移动到另一个选项卡 - Google App Script: move row (only selected columns within the row) to another tab based on value in column 如何在谷歌应用程序脚本的 forEach 循环中设置谷歌表格中目标单元格的值? - How can you set the value of a target cell in google sheets within a forEach loop in google apps script? 如何通过脚本从谷歌表格中的单元格中删除子字符串 - How to remove substring from the cell in google sheets via script 将范围作为全局变量返回 Google 表格脚本 - Return range as a global variable Google sheets script 如何在 Google 表格中为宏(google-sheets、filter、if、else、script)中的特定激活值空列过滤器使用 if 条件? - How to use if condition for specific activated value empty column filter in Google Sheets for macro (google-sheets, filter, if, else, script)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM