简体   繁体   English

如何使用 Google Apps 脚本将 2 个多维 arrays 替换为来自 arrray2 的一些 array1 的元素?

[英]How to combine 2 multidimensional arrays replacing some of array1's element with the one from arrray2 using Google Apps Script?

I've been trying to get this one done, without any success.我一直在努力完成这个,但没有成功。

This is data to be placed where there is no formula.这是要放置在没有公式的地方的数据。 So, the idea is to merge these two arrays and place it at once.所以,想法是合并这两个 arrays 并立即放置。

array1 = 
[
 ["item1","","","details1"], 
 ["item2","","","details2"], 
]

This array contains formulas grabbed from the destination range where the incoming data will be placed, but since the formulas are needed,该数组包含从将放置传入数据的目标范围中获取的公式,但由于需要公式,

array2 = 
[
 ["","=iferror(VLOOKUP(A63,'Client List'!$A$1:$S,19,0),"")","=iferror(if(B63="Agency", 'Reference Info'!$C$7, VLOOKUP(A63,'Client List'!$A$1:$T,20,0)),"")",""], 
 ["","=iferror(VLOOKUP(A64,'Client List'!$A$1:$S,19,0),"")","=iferror(if(B64="Agency", 'Reference Info'!$C$7, VLOOKUP(A64,'Client List'!$A$1:$T,20,0)),"")",""]
]

Expected Result预期结果

array2 = 
[
 ["item1","=iferror(VLOOKUP(A63,'Client List'!$A$1:$S,19,0),"")","=iferror(if(B63="Agency", 'Reference Info'!$C$7, VLOOKUP(A63,'Client List'!$A$1:$T,20,0)),"")","details1"], 
 ["item2","=iferror(VLOOKUP(A64,'Client List'!$A$1:$S,19,0),"")","=iferror(if(B64="Agency", 'Reference Info'!$C$7, VLOOKUP(A64,'Client List'!$A$1:$T,20,0)),"")","details2"]
]

This is my attempt, but I can't seem to get to the bottom of it:这是我的尝试,但我似乎无法深入了解它:

 let finalRowValues = []
  for (let a = 0; a < array2.length; a++) {
    for (let n = 0; n < array1.length; n++) {
      array2[a].forEach(function(value, j){
        if(value == '' && array1[n][j] != ''){
          finalRowValues.push(array1[n][j])
        } else {
          finalRowValues.push(value)
        }
      })
    }
  }

Here's a simple way to insert the formula if no value is present:如果没有值,这是插入公式的简单方法:

function myFunction() {

  const formulas = [[]]
  const values = [[]]

  return values.map((row, rowIndex) => row.map((col, colIndex) => col || formulas[rowIndex][colIndex]))

}

This is one of the way's to do it.这是做到这一点的方法之一。

const array1 = 
[
 ["item1","","","details1"], 
 ["item2","","","details2"], 
]

const array2 = 
[
 ["",`=iferror(VLOOKUP(A63,'Client List'!$A$1:$S,19,0),"")`,`=iferror(if(B63="Agency", 'Reference Info'!$C$7, VLOOKUP(A63,'Client List'!$A$1:$T,20,0)),"")`,""], 
 ["",`=iferror(VLOOKUP(A64,'Client List'!$A$1:$S,19,0),"")`,`=iferror(if(B64="Agency", 'Reference Info'!$C$7, VLOOKUP(A64,'Client List'!$A$1:$T,20,0)),"")`,""]
]

const result = array1.map((arr, i) => {
  const [place1, place2, place3, place4] = arr
  if(place2 == ""){
    return [place1, array2[i][1], array2[i][2],place4]
  } else {
    return arr
  }
})

console.log(result)

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

相关问题 如何以array1的第一个元素和array2的第一个元素等方式组合2个数组形成一个新数组 - How to combine 2 arrays in a way that 1st element of array1 and 1st element of array2 and so on form a new array 使用loadash或一些本机方法检查array1是否包含array2中的任何元素? - check if array1 contains any element from array2, using loadash or some native method? 如何使用来自不同一维数组的数据过滤二维数组 - Javascript/Google Apps 脚本? - How to filter a two dimensional array using data from a different one dimensional array - Javascript/Google Apps Script? 将2个数组组合成一个多维数组? - Combine 2 arrays into a multidimensional array? 如何从 arrays 的数组中提取一个值 - How to extract from an arrray of arrays one single value 如何在Google Apps脚本中为数组编制索引? - How to index array's in Google Apps Script? 使用Google Apps脚本替换数组中的变音符号 - Replacing Diacritics in an Array with Google Apps Script 使用数组元素从 object 数组中查找 object 并将其替换 - Find an object from array of object using an arrray element and replace it Google Apps 脚本:当 Arrays 之间存在匹配时,合并 Arrays - Google Apps Script: Combine Arrays When there is A Match Between Both Arrays 将 Arrays 组合为列而不是行的 Google Apps 脚本 - Google Apps Script To Combine Arrays as Columns Instead of Rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM