【发布时间】: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