【问题标题】:Problems to get only the value of a row within a 2D array Google Apps Script仅获取二维数组 Google Apps 脚本中一行的值的问题
【发布时间】:2021-07-11 08:15:11
【问题描述】:

在我的工作表中,我的范围是 E10:E34,其中包含 URL。

我也有包含引用的范围 C10:C34。

在 I10:I34 范围内,应采用以下公式:

=IF(SEARCH("URL";URL cell A1notation);"reference";"")

这个公式,转换成真实数据后,看起来像这样;

=IF(SEARCH("https://collaresmolones.com/wp-content/uploads/2021/01/Mi-vecino-Totoro.jpg";E10);"322";"")

我正在使用 Google Apps 脚本,以便它使用每行的数据自动转换公式,即使用 URL、URL 的 A1notation 单元格和引用的值。

这是我目前的代码:

function ref_URL_formula3(){

  var libro = SpreadsheetApp.getActiveSpreadsheet();
  var hoja = libro.getSheetByName("ImportXML");

  var rangoURLvalor = hoja.getRange('E10:E34').getValues();
  var colURL = 'E';
  var rangoURLnotationCelda = hoja.getRange('E10:E34').getRow();
  var rangoREFvalor = hoja.getRange('C10:C34').getDisplayValues();
  var rangoFormula = hoja.getRange('I10:I34');

  rangoURLvalor.flat().forEach((v, i) => {
      if (v == rangoURLvalor[i][0]) rangoFormula.setFormula('=IF(SEARCH("'+ rangoURLvalor +'";'+ colURL+rangoURLnotationCelda +');"'+ rangoREFvalor +'";"")')
    })
 }

在公式中,在 URL 中和在 Reference 中它不返回行的唯一值,否则我得到整个范围的所有值。

具体来说,在 I10:I34 范围的单元格中,这是我得到的:

=IF(SEARCH("https://collaresmolones.com/wp-content/uploads/2021/01/Mi-vecino-Totoro.jpg,https://collaresmolones.com/wp-content/uploads/2021/01/Kodama-Princesa-Mononoke.jpg,https://collaresmolones.com/wp-content/uploads/2021/01/Coco-and-friends.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Dragon-Ball.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/El-Rey-Leon.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/El-Rey-Leon-2.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/El-Rey-Leon-3.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/El-Rey-Leon-4.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Flamingo.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Harry-Potter-1.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Harry-Potter-2.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Harry-Potter-Snitch-Dorada.jpg,https://collaresmolones.com/wp-content/uploads/2021/01/Harry-Potter-2.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Harry-Potter-Griffindor.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Minnie.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Star-Wars-2.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Supercats-Hero.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Vis-a-vis-2.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Super-mario.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Gat-y-gos-Gato-y-perro-CatDog.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Rugrats.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Kimetsu-no-Yaiba.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Inuyasha.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Fire.jpg,https://collaresmolones.com/wp-content/uploads/2020/10/Blue.jpg";E10);"322,323,321,301,303,302,304,319,305,306,307,315,324,308,300,309,311,317,310,316,312,313,314,318,320";"")

当我想要得到的是另一个时:

=IF(SEARCH("https://collaresmolones.com/wp-content/uploads/2021/01/Mi-vecino-Totoro.jpg";E10);"322";"")

很明显行的路径有错误,但是,我真的不知道我做错了什么。

我是 Google 表格和 Google Apps 脚本的新手,非常感谢所有帮助。

谢谢。

【问题讨论】:

  • 为什么要使用脚本而不是一次性填满一列的数组公式?这是XY problem 吗?考虑共享一个可公开编辑的sample spreadsheet,其中包含逼真的数据,并在那里显示您手动输入的预期结果。
  • 我想用谷歌应用程序脚本来做这件事是因为我需要获取单元格的可见内容,而不是单元格本身。我认为这不能通过公式获得......例如,如果我通过公式进行,它看起来像这样:``` =IF(SEARCH(E10;E10);C10;"") ``` 和我真正需要的是值,所以它看起来像这样:``` =IF(SEARCH("collaresmolones.com/wp-content/uploads/2021/01/…) `` @doubleunary
  • 您可以通过rept(E10, 1)获取单元格中的可见内容。考虑共享一个示例电子表格。
  • 您可以使用数组公式遍历范围内的行,并使用if()ifs() 选择要在每行中返回的值。脚本也可以做到这一点,但这样做似乎没有什么意义。考虑通过共享示例电子表格来展示您拥有的以及您正在尝试完成的工作。

标签: google-apps-script google-sheets


【解决方案1】:

您可以使用 setFormulas() 函数将所有公式排列在一个 map 函数中,并将它们放在一个范围内。

function ref_URL_formula3(){

  var libro = SpreadsheetApp.getActiveSpreadsheet();
  var hoja = libro.getActiveSheet();

  var rangoURLvalor = hoja.getRange('E10:E34').getValues();
  var rangoREFvalor = hoja.getRange('C10:C34').getValues();
  var rangoFormula = hoja.getRange('I10:I34');

  var formulas = rangoURLvalor.map(function (v,i) {

    return ['IF(SEARCH("' + v + '"; E' + (i + 10) + ');' + rangoREFvalor[i] +';"")']

  })

  rangoFormula.setFormulas(formulas) 

}

【讨论】:

  • 非常感谢您的帮助!这绝对解决了问题。您使我不必在 300 多行中手动执行此操作。现在我看到代码中的行遍历完全错误。毫无疑问,我今天学到了一些新东西。再次,非常感谢!我将此答案标记为有效并投赞成票。 @MuhammetYunusTunca
  • 不客气。我很高兴我在这个网站上的第二个贡献已经建立起来很有帮助。 :) 让我再解释一下“地图”方法。我们用它来操作一个数组。它遍历数组的所有项,执行我们希望的更改,并返回包含这些新项的新数组。最后一件事:我不太明白你为什么需要这样的东西。也许如果您与我们分享电子表格链接,我们可以为您提供更多帮助。一切顺利。
猜你喜欢
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
  • 2013-11-05
  • 1970-01-01
相关资源
最近更新 更多