英文:
how to reproduce an action on all the members of a list to write an "importrange" command from multiple http links
问题
I have translated the provided text. Here is the translated content:
我正试图使用“=importrange”命令和脚本将许多Google表格导入到一个表格中。
到目前为止,这是我卡住的地方:
我已经能够编写代码以导入一个表格,但是当涉及导入和处理2个或更多表格时,我不再能够“迭代”我的操作。
以下是一个示例,其中包含3个要写入框中的http链接:
=QUERY({importrange("https://docs.google.com/spreadsheets/d/1ZvgM-rTpHJbBJB-F6iqtSE7NB5Jdl-_Q7LQL_ELhLo8?authuser=design%40wearemakers.fr&usp=drive_fs","'TOTAUX'!A3:C");importrange("https://docs.google.com/spreadsheets/d/1SH9iY55UEm9glHXfgFD2bpMCcDaQMChS8nsH_0SJ6RA?authuser=design%40wearemakers.fr&usp=drive_fs","'TOTAUX'!A3:C");https://docs.google.com/spreadsheets/d/1SH9iY55UEm9glHXfgFD2bpMCcDaQMChS8nsH_0SJ6RA/edit?usp=drivesdk","'TOTAUX'!A3:C")}, "Select * WHERE Col2 IS NOT NULL")
以下是代码目前生成的内容:
=QUERY({importrange("https://docs.google.com/spreadsheets/d/174wJ6l1ohnYKxaKEQLjOiCprc1bwqlY0dougaLZqOs4/edit?usp=drivesdk,https://docs.google.com/spreadsheets/d/1ZvgM-rTpHJbBJB-F6iqtSE7NB5Jdl-_Q7LQL_ELhLo8/edit?usp=drivesdk,https://docs.google.com/spreadsheets/d/1SH9iY55UEm9glHXfgFD2bpMCcDaQMChS8nsH_0SJ6RA/edit?usp=drivesdk,,,,,,,,,","'TOTAUX'!A3:C")}, "Select * WHERE Col2 IS NOT NULL")
以下是代码:
function IMPORT_002() {
var COLsheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("COLLECTEUR");
var link = COLsheet.getRange(2,9,COLsheet.getMaxRows()).getValues();
var plage = "'TOTAUX'!A3:C";
var imp = 'importrange("' + (link)+ '","' + (plage) + '")';
var que = '=QUERY({' + (imp) + '}, "Select * WHERE Col2 IS NOT NULL")';
Logger.log(link)
COLsheet.getRange('D5').setValues([[que]]);
}
我已提供翻译的文本,请告诉我如果您需要进一步的帮助。
英文:
I'am trying to import a lot of Googlesheet into 1 using the "=importrange" command and a script.
here is so far where I'am getting stuck :
I have been able to do the code to import 1 sheet, but when it comes to import and deal with 2 or more sheets, I'am no longer able to "iterate" my actions.
here is an example with 3 http links of what I want to write into the box :
=QUERY({importrange("https://docs.google.com/spreadsheets/d/1ZvgM-rTpHJbBJB-F6iqtSE7NB5Jdl-_Q7LQL_ELhLo8?authuser=design%40wearemakers.fr&usp=drive_fs","'TOTAUX'!A3:C");importrange("https://docs.google.com/spreadsheets/d/1SH9iY55UEm9glHXfgFD2bpMCcDaQMChS8nsH_0SJ6RA?authuser=design%40wearemakers.fr&usp=drive_fs","'TOTAUX'!A3:C");https://docs.google.com/spreadsheets/d/1SH9iY55UEm9glHXfgFD2bpMCcDaQMChS8nsH_0SJ6RA/edit?usp=drivesdk","'TOTAUX'!A3:C")}, "Select * WHERE Col2 IS NOT NULL")
here is what the code produce actually :
=QUERY({importrange("https://docs.google.com/spreadsheets/d/174wJ6l1ohnYKxaKEQLjOiCprc1bwqlY0dougaLZqOs4/edit?usp=drivesdk,https://docs.google.com/spreadsheets/d/1ZvgM-rTpHJbBJB-F6iqtSE7NB5Jdl-_Q7LQL_ELhLo8/edit?usp=drivesdk,https://docs.google.com/spreadsheets/d/1SH9iY55UEm9glHXfgFD2bpMCcDaQMChS8nsH_0SJ6RA/edit?usp=drivesdk,,,,,,,,,","'TOTAUX'!A3:C")}, "Select * WHERE Col2 IS NOT NULL")
And here the code :
var COLsheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("COLLECTEUR");
var link = COLsheet.getRange(2,9,COLsheet.getMaxRows()).getValues();
var plage = "'TOTAUX'!A3:C";
var imp = 'importrange("' + (link)+ '";"' + (plage) + '")';
var que = '=QUERY({' + (imp) + '}, "Select * WHERE Col2 IS NOT NULL")';
/**
for (var i in link) {
var links = link[i];
}**/
Logger.log(link)
//Logger.log(links)
COLsheet.getRange('D5').setValues([[que]]);
} ```
I have try to do something with[i], but I just don't really know what to do...
</details>
# 答案1
**得分**: 2
虽然我不确定我是否能正确理解您期望的结果,不过以下修改如何?
### 修改后的脚本:
```javascript
function myFunction() {
var COLsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("COLLECTEUR");
var link = COLsheet.getRange(2, 9, COLsheet.getLastRow() - 1).getDisplayValues().filter(([i]) => i);
var plage = "'TOTAUX'!A3:C";
var imp = link.map(([i]) => `importrange("${i}";"${plage}")`);
var que = `=QUERY({${imp.join(";")}},"Select * WHERE Col2 IS NOT NULL")`;
COLsheet.getRange('D5').setFormula(que);
}
-
当运行此脚本时,如果
link
的值是有效的电子表格URL,que
将变为以下内容。=QUERY({
importrange("https://docs.google.com/spreadsheets/d/###/edit";"'TOTAUX'!A3:C");
importrange("https://docs.google.com/spreadsheets/d/###/edit";"'TOTAUX'!A3:C");
importrange("https://docs.google.com/spreadsheets/d/###/edit";"'TOTAUX'!A3:C");
;
;
;
},"Select * WHERE Col2 IS NOT NULL") -
在
var link = COLsheet.getRange(2,9,COLsheet.getMaxRows()).getValues()
的情况下,将从所有行检索值。因此,我修改为var link = COLsheet.getRange(2, 9, COLsheet.getLastRow()).getValues().filter(([i]) => i)
。 -
在此修改中,使用了模板文字。
-
为了创建
importrange
的公式,我使用了map
。
参考:
英文:
Although I'm not sure whether I could correctly understand your expected result, how about the following modification?
Modified script:
function myFunction() {
var COLsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("COLLECTEUR");
var link = COLsheet.getRange(2, 9, COLsheet.getLastRow() - 1).getDisplayValues().filter(([i]) => i);
var plage = "'TOTAUX'!A3:C";
var imp = link.map(([i]) => `importrange("${i}";"${plage}")`);
var que = `=QUERY({${imp.join(";")}},"Select * WHERE Col2 IS NOT NULL")`;
COLsheet.getRange('D5').setFormula(que);
}
-
When this script is run, when the values of
link
are the valid Spreadsheet URLs,que
becomes as follows.=QUERY({ importrange("https://docs.google.com/spreadsheets/d/###/edit","'TOTAUX'!A3:C"); importrange("https://docs.google.com/spreadsheets/d/###/edit","'TOTAUX'!A3:C"); importrange("https://docs.google.com/spreadsheets/d/###/edit","'TOTAUX'!A3:C"); ; ; ; },"Select * WHERE Col2 IS NOT NULL")
-
In the case of
var link = COLsheet.getRange(2,9,COLsheet.getMaxRows()).getValues()
, the values are retrieved from all rows. So, I modified tovar link = COLsheet.getRange(2, 9, COLsheet.getLastRow()).getValues().filter(([i]) => i)
. -
In this modification, the template literal was used.
-
In order to create the formula of
importrange
, I usedmap
.
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论