英文:
getValues From getRangeList
问题
我的脚本运行速度非常慢,因为我正在逐个从许多不同范围中检索值来执行特定操作。
我已经进行了研究,发现了getRangeList
。
我想要做的是将所有这些值放入一个数组中:
var 1 = sheet.getRange('A1:B1').getValues()
var 2 = sheet.getRange('A2:B2').getValues()
var 3 = sheet.getRange('A3:B3').getValues()
以减少 API 调用,就像这样:
var array = [1, 2, 3]
然后使用:
array[0]
array[1]
array[2]
进行特定的操作。
目前这是我的代码:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Meta Awareness Campaign');
var metacampaignvalues = sheet.getRangeList(['A3:AM3','A4:AM4','A5:AM5','A6:AM6','A7:AM7','A8:AM8','A9:AM9','A10:AM10','A11:AM11','A12:AM12','A13:AM13','A14,AM14','A15:AM15','A16:AM16','A17:AM17','A18:AM18','A19:AM19','A20:AM20']).getRanges().map(range => range.getValues());
所以我需要使用:
metacampaignvalues[0] = sheet.getRange('A3:M3').getValues()
metacampaignvalues[1] = sheet.getRange('A4:M4').getValues()
metacampaignvalues[2] = sheet.getRange('A5:M5').getValues()
等等...
在当前状态下,我的代码出现错误,显示"异常:未找到范围"。
谢谢你的帮助。
英文:
My script is running very slow because I am retrieving values from many different ranges one by one to apply a specific execution.
I have researched and found out about the getRangeList.
What I would like to do is put in an array all my
var 1 = sheet.getRange('A1:B1').getValues()
var 2 = sheet.getRange('A2:B2').getValues()
var 3 = sheet.getRange('A3:B3').getValues()
to reduce API calls, like such
var array = [1,2,3]
and then use
array[0]
array[1]
array[2]
for specific actions.
Currently this is my code:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Meta Awareness Campaign');
var metacampaignvalues = sheet.getRangeList(['A3:AM3','A4:AM4','A5:AM5','A6:AM6','A7:AM7','A8:AM8','A9:AM9','A10:AM10','A11:AM11','A12:AM12','A13:AM13','A14,AM14','A15:AM15','A16:AM16','A17:AM17','A18:AM18','A19:AM19','A20:AM20']).getRanges().map(range => range.getValues());
So I need to use
metacampaignvalues[0] = sheet.getRange('A3:M3').getValues()
metacampaignvalues[1] = sheet.getRange('A4:M4').getValues()
metacampaignvalues[2] = sheet.getRange('A5:M5').getValues()
etc...
In its current state, my code is receiving an error saying: Exception: Range Not Found.
Thank you for your help.
答案1
得分: 0
我认为当我看到你的脚本时,需要将'A14,AM14'
修改为'A14:AM14'
。我认为这可能是你当前出现Exception: Range Not Found
问题的原因。
在修改脚本时,请按照以下方式进行修改。
从:
var metacampaignvalues = sheet.getRangeList(['A3:AM3','A4:AM4','A5:AM5','A6:AM6','A7:AM7','A8:AM8','A9:AM9','A10:AM10','A11:AM11','A12:AM12','A13:AM13','A14,AM14','A15:AM15','A16:AM16','A17:AM17','A18:AM18','A19:AM19','A20:AM20']).getRanges().map(range => range.getValues());
到:
var metacampaignvalues = sheet.getRangeList(['A3:AM3', 'A4:AM4', 'A5:AM5', 'A6:AM6', 'A7:AM7', 'A8:AM8', 'A9:AM9', 'A10:AM10', 'A11:AM11', 'A12:AM12', 'A13:AM13', 'A14:AM14', 'A15:AM15', 'A16:AM16', 'A17:AM17', 'A18:AM18', 'A19:AM19', 'A20:AM20']).getRanges().map(range => range.getValues());
- 但是,在你的脚本中,行号似乎是连续的数字。所以,以下修改如何?
var metacampaignvalues = sheet.getRange('A3:AM20').getValues();
- 通过这个修改,例如,
metacampaignvalues[5]
将返回A8:AM8
的值。
注意:
- 作为另一种方法,如果你想以低处理成本从非连续行号中检索值,可以考虑使用Sheets API,如下所示。在测试这个脚本时,请启用Google高级服务中的Sheets API。
function sample() {
const sheetName = 'Meta Awareness Campaign';
const a1Notations = ['A1:C1', 'A3:C3', 'A5:C5'];
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ranges = a1Notations.map(e => `'${sheetName}'!${e}`);
const res = Sheets.Spreadsheets.Values.batchGet(ss.getId(), { ranges }).valueRanges.reduce((o, e) => (o[e.range.split("!")[1]] = e.values, o), {});
console.log(res);
}
- 通过这个示例脚本,你可以检索到以下对象。
{
"A1:C1":[["A1","B1","C1"]],
"A3:C3":[["A3","B3","C3"]],
"A5:C5":[["A5","B5","C5"]]
}
英文:
I think that when I saw your script, 'A14,AM14'
is required to be modified to 'A14:AM14'
. I thought that this might be the reason for your current issue of Exception: Range Not Found.
.
When your script is modified, please modify as follows.
From:
var metacampaignvalues = sheet.getRangeList(['A3:AM3','A4:AM4','A5:AM5','A6:AM6','A7:AM7','A8:AM8','A9:AM9','A10:AM10','A11:AM11','A12:AM12','A13:AM13','A14,AM14','A15:AM15','A16:AM16','A17:AM17','A18:AM18','A19:AM19','A20:AM20']).getRanges().map(range => range.getValues());
To:
var metacampaignvalues = sheet.getRangeList(['A3:AM3', 'A4:AM4', 'A5:AM5', 'A6:AM6', 'A7:AM7', 'A8:AM8', 'A9:AM9', 'A10:AM10', 'A11:AM11', 'A12:AM12', 'A13:AM13', 'A14:AM14', 'A15:AM15', 'A16:AM16', 'A17:AM17', 'A18:AM18', 'A19:AM19', 'A20:AM20']).getRanges().map(range => range.getValues());
-
But, in your script, it seems that the row numbersa are continuous numbers. So, how about the following modification?
var metacampaignvalues = sheet.getRange('A3:AM20').getValues();
- By this modification, for example,
metacampaignvalues[5]
returns the values ofA8:AM8
.
- By this modification, for example,
Note:
-
As another approach, if you want to retrieve the values from non-continuous row numbers like
'A1:C1', 'A3:C3', 'A5:C5'
with low process cost, how about using Sheets API as follows? When you test this script, please enable Sheets API at Advanced Google services.function sample() { const sheetName = 'Meta Awareness Campaign'; const a1Notations = ['A1:C1', 'A3:C3', 'A5:C5']; const ss = SpreadsheetApp.getActiveSpreadsheet(); const ranges = a1Notations.map(e => `'${sheetName}'!${e}`); const res = Sheets.Spreadsheets.Values.batchGet(ss.getId(), { ranges }).valueRanges.reduce((o, e) => (o[e.range.split("!")[1]] = e.values, o), {}); console.log(res); }
-
By this sample script, you can retrieve an object as follows.
{ "A1:C1":[["A1","B1","C1"]], "A3:C3":[["A3","B3","C3"]], "A5:C5":[["A5","B5","C5"]] }
-
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论