英文:
Google Apps Script custom function - 2nd parameter array is empty
问题
已确认函数的逻辑按预期使用了函数内的数组。但是,一旦我将数组作为参数传递,第二个参数出现了问题。
基本上,该函数遍历一个2D数组,这个数组是一个日历,找到最近的活动,然后返回alt_activity范围中的下一个值。如果前一个活动是alt_activity范围的最后一个值,那么再次从alt_activity范围的开头开始。
示例数据
/**
* 在日历的最后匹配值中查找替代锻炼。
* @param {array} input 要搜索的日历数组。
* @return 从@param的最后一行(WEEK)和列(DAY)开始的第一个匹配项。
* @customfunction
*/
function calendar_search(calendar_range, alt_activities_range)
{
/**var calendar_range = [[ , , , , , 'Chess'],
[ , , , , 'Pilates', 'Elevate'],
[ , , , 'Kundalini', 'Scrabble', 'Gardening'],
[ , , 'Darning', 'Crossword', 'Chess', 'Pilates']];
*/
//var alt_activities_range = ['Chess', 'Pilates', 'Elevate', 'Kundalini', 'Scrabble', 'Gardening', 'Darning', 'Crossword'];
for (let week = calendar_range.length - 1; week >= 0; week--)
{
for (let day = calendar_range[week].length - 1; day >= 0; day--)
{
//return calendar_range[week][day]; // 测试功能到达的位置。
return alt_activities_range[day]; // 作为参数传递时,数组为空
if (alt_activities_range.indexOf(calendar_range[week][day]) > -1)
{
//未进入此if语句!!! return (JSON.stringify(calendar_range));
/**
if (calendar_range[week][day] === alt_activities_range[alt_activities_range.length - 1])
{
return alt_activities_range[0];
}
// 转到匹配的索引加1以返回下一个值
return alt_activities_range[(alt_activities_range.indexOf(calendar_range[week][day]) + 1) % alt_activities_range.length];
}
*/
}
}
}
}
表格1
列A | 列B | 列C | 列D | 列E | 列F | 列G | 列H |
---|---|---|---|---|---|---|---|
替代锻炼 | Chess | Pilates | Elevate | Kundalini | Scrabble | Gardening | Darning |
表格I|
Crossword|
表格2
列A | 列B | 列C | 列D | 列E | 列F | 列G |
---|---|---|---|---|---|---|
DAY 1 | DAY 2 | DAY 3 | DAY 4 | DAY 5 | DAY 6 | |
WEEK 1 | Chess | |||||
WEEK 2 | Pilates | Elevate | ||||
WEEK 3 | Kundalini | Scrabble | Gardening | |||
WEEK 4 | Darning | Crossword | Chess | Pilates |
英文:
Confirmed the logic of the function worked as expected by using arrays within the function.
However, once I passed arrays as parameters issue with the 2nd parameter reared up it's head.
Basically, the function loops through a 2D array that's a calendar, find the last (most recent) activity and then return the next value from the alt_activity range. If the previous activity is the last value from the alt_activity range, start from the start of the alt_activity range once more.
Sample data
/**
* Looks for the alternate workout in the LAST matching value within the calendar.
* @param {array} input The calendar array to be searched.
* @return The first match starting from the last row (WEEK) and column (DAY) of the @param.
* @customfunction
*/
function calender_search(calendar_range, alt_activities_range)
{
/**var calendar_range = [[ , , , , , 'Chess'],
[ , , , , 'Pilates', 'Elevate'],
[ , , , 'Kundalini', 'Scrabble', 'Gardening'],
[ , , 'Darning', 'Crossword', 'Chess', 'Pilates']];
*/
//var alt_activities_range = [ 'Chess', 'Pilates', 'Elevate', 'Kundalini', 'Scrabble', 'Gardening', 'Darning', 'Crossword'];
for (let week = calendar_range.length - 1; week >= 0; week--)
{
for (let day = calendar_range[week].length - 1; day >= 0; day--)
{
//return calendar_range[week][day]; // test where the functionality gets to.
return alt_activities_range[day]; // array is empty when passed in as a parameter
if (alt_activities_range.indexOf(calendar_range[week][day]) > -1)
{
//NOT entering this if statement!!! return (JSON.stringify(calendar_range));
/**
if (calendar_range[week][day] === alt_activities_range[alt_activities_range.length - 1])
{
return alt_activities_range[0];
}
// Go to index of match PLUS 1 for NEXT value to be returned
return alt_activities_range[(alt_activities_range.indexOf(calendar_range[week][day]) + 1) % alt_activities_range.length];
}
*/
}
}
}
}
Table 1
Column A | Column B | Column C | Column D | Column E | Column F | Column G | Column H |
---|---|---|---|---|---|---|---|
Wrkout Alternatives | Chess | Pilates | Elevate | Kundalini | Scrabble | Gardening | Darning |
Column I |
---|
Crossword |
Table 2
Column A | Column B | Column C | Column D | Column E | Column F | Column G |
---|---|---|---|---|---|---|
DAY 1 | DAY 2 | DAY 3 | DAY 4 | DAY 5 | DAY 6 | |
WEEK 1 | Chess | |||||
WEEK 2 | Pilates | Elevate | ||||
WEEK 3 | Kundalini | Scrabble | Gardening | |||
WEEK 4 | Darning | Crossword | Chess | Pilates |
答案1
得分: 1
I figured out a (crude) work around for the issue. I copied the alt_wrkout_range parameter to a local variable (alt_wrkout_ARRAY). The first mention of the al_wrkout_range is within an IF clause so I'm guessing that this is where things stop working. An explicity variable seems to do the trick.
/**
-
Looks for the alternate workout in the LAST matching value within the calendar.
-
@param {array} input The calendar array to be searched.
-
@return The first match starting from the last row (WEEK) and column (DAY) of the @param.
-
@customfunction
*/
function calender_search(calendar_range, alt_activities_range)
{
var alt_activities_array=alt_activities_range[0].slice();for (let week = calendar_range.length - 1; week >= 0; week--)
{
for (let day = calendar_range[week].length - 1; day >= 0; day--)
{if (alt_activities_array.indexOf(calendar_range[week][day]) > -1) { if (calendar_range[week][day] === alt_activities_array[alt_activities_array.length - 1]) { return alt_activities_array[0]; } // Go to index of match PLUS 1 for NEXT value to be returned return alt_activities_array[(alt_activities_array.indexOf(calendar_range[week][day]) + 1) % alt_activities_array.length]; }
}
}
}
Perhaps someone will explain why the IF statement was causing the issue.
英文:
I figured out a (crude) work around for the issue. I copied the alt_wrkout_range parameter to a local variable (alt_wrkout_ARRAY). The first mention of the al_wrkout_range is within an IF clause so I'm guessing that this is where things stop working. An explicity variable seems to do the trick.
/**
* Looks for the alternate workout in the LAST matching value within the calendar.
* @param {array} input The calendar array to be searched.
* @return The first match starting from the last row (WEEK) and column (DAY) of the @param.
* @customfunction
*/
function calender_search(calendar_range, alt_activities_range)
{
var alt_activities_array=alt_activities_range[0].slice();
for (let week = calendar_range.length - 1; week >= 0; week--)
{
for (let day = calendar_range[week].length - 1; day >= 0; day--)
{
if (alt_activities_array.indexOf(calendar_range[week][day]) > -1)
{
if (calendar_range[week][day] === alt_activities_array[alt_activities_array.length - 1])
{
return alt_activities_array[0];
}
// Go to index of match PLUS 1 for NEXT value to be returned
return alt_activities_array[(alt_activities_array.indexOf(calendar_range[week][day]) + 1) % alt_activities_array.length];
}
}
}
}
Perhaps someone will explain why the IF statement was causing the issue
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论