简体   繁体   English

Apps 脚本 - 比较两个数组中的字符串

[英]Apps Script - compare strings in two arrays

I am a stuck on this piece of code.我被困在这段代码上。 I have an Example sheet with two tabs.我有一个带有两个选项卡的示例表 The first tab is new items.第一个选项卡是新项目。 A new item is comprised of two pieces, a attribute code (string), and an item ID (number).一个新项目由两部分组成,一个属性代码(字符串)和一个项目 ID(数字)。 In the other tab "Locations" there are a bunch of empty locations.在另一个选项卡“位置”中有一堆空位置。 Each location has primary attribute (string), and a set of secondary attribute codes in a longer string.每个位置都有主要属性(字符串)和较长字符串中的一组次要属性代码。

I have assigned these two ranges to two unique arrays.我已将这两个范围分配给两个唯一的数组。

function matchcodes() {

var locss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Locations');
var lastlocRow = locss.getLastRow();
var newitems = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('New Items');
var lastNIRow = newitems.getLastRow();
var itemcodes = newitems.getRange("A1:B" + lastNIRow).getValues();
var locations = locationssheet.getRange("A2:D" + lastlocationRow).getValues();
Logger.log(itemcodes)
Logger.log(locations)}

What I am attempting to do is compare itemcodes[i][0] to locations[j][2] (match item attribute with location primary attribute).我正在尝试做的是将itemcodes[i][0]locations[j][2]进行比较(将 item 属性与 location 主要属性匹配)。 If the strings match I want to copy itemcodes[i][1] (ItemID) and set it as the value of locations[j][1] .如果字符串匹配,我想复制itemcodes[i][1] (ItemID) 并将其设置为locations[j][1]的值。 If the strings do not match check the next iteration of locations[j][2] .如果字符串不匹配,请检查locations[j][2]的下一次迭代。 If no matching attributes are found in locations[j][2] , I would like to see if it is contained as a substring in locations[j][3] (starting back at the top and iterating through the whole list of secondary attributes. If the substring code is contained in loactions[j][3] I would like take the same action in the first IF condition.如果在locations[j][2]中没有找到匹配的属性,我想看看它是否作为一个子字符串包含在locations[j][3]中(从顶部开始并遍历整个辅助属性列表. 如果子字符串代码包含在loactions[j][3]我想在第一个 IF 条件中采取相同的操作。

Once a new item is matched, the loop can break, and the next item can be located itemcodes[i+1][0] .一旦匹配了一个新项目,循环就会中断,下一个项目可以位于itemcodes[i+1][0] If no match is found in the primary or secondary search, also iterate to the next new item.如果在主要或次要搜索中未找到匹配项,则还迭代到下一个新项目。

Where I'm struggling is writing the condition statements to compare both strings and substrings within strings.我正在努力编写条件语句来比较字符串中的字符串和子字符串。

//for (var i = 0; i < itemcodes.length; i++) {
   //for (var j = 0; j < locations.length; j++) {
    //if (itemcodes[i][0] == locations[j][2]) {
      // I want set the value of locations[j][1] with itemcodes[i][1] 
    }
     // if no match is found in entire [j][2] column, search for substring in locations[j][3] column
    //if item match is found, or no match is found in all of [j][2] or [j][3] break loop and iterate to [i+1][0] and start the next loop

Input (3 iterations) Results输入(3 次迭代)结果

Any help would be much appreciated.任何帮助将非常感激。 Or if you can point me to a similar thread.或者,如果你能指出我类似的线程。 (I've not had any success finding a similar example) Thanks in advance! (我没有成功找到类似的例子)提前谢谢!

Try:尝试:

function matchCodes() {

  const newItems = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(`New Items`)
  const locations = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(`Locations`)

  const newItemsValues = newItems.getDataRange().getValues()
  const locationsValues = locations.getDataRange().offset(1, 0).getValues()

  newItemsValues.forEach(([attribute, id]) => {

    const primaryTarget = locationsValues.findIndex(row => row[2] === attribute && row[1] === ``)
    if (primaryTarget !== -1) return locationsValues[primaryTarget][1] = id

    const secondaryTarget = locationsValues.findIndex(row => row[3].includes(attribute) && row[1] === ``)
    if (secondaryTarget !== -1) return locationsValues[secondaryTarget][1] = id

  })

  locations.getDataRange().offset(1, 0).setValues(locationsValues)

}

Learn More:学到更多:

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

相关问题 比较两个字符串数组 - Compare two Strings Arrays 获取两个数组,比较并删除 Google Apps 脚本中的重复项 - Get two arrays, compare and remove duplicates in Google Apps Script Apps 脚本 - 比较两个 arrays,找出差异(单元格的值和索引) - Apps Script - compare two arrays, find differences (value and index of cells) 比较两个数组,并仅获取与javascript / google-apps-script中的第1列数据匹配的数组 - Compare two arrays and obtain only one with data matching colum 1 in javascript/ google-apps-script 如何比较两个 2d Arrays 并按特定顺序返回结果(Google Apps 脚本)? - How to compare two 2d Arrays and return the result in a specific order (Google Apps Script)? 如何在javascript中比较两个数组或字符串? - How to compare two arrays or strings in javascript? 将两个整数数组与Google Apps脚本进行比较 - Comparing two arrays of integers with Google Apps Script 比较两个String数组,分别返回匹配的String和不匹配的String - Compare two String arrays , return matched Strings and mismatched Strings separately 如何在 Shell 脚本中比较两个 Arrays - How to Compare Two Arrays in Shell Script Javascript / Google Apps Script比较2个数组,返回没有匹配项 - Javascript / Google Apps Script compare 2 arrays, return items with NO match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM