一个复杂函数的想法,它在一个排序的整数列表中找到一个不大于给定数字的数字
An idea for a complex function that finds in a sorted list of integers a number not bigger than a given one
比如说,我在 A1 中有一个文本,其中包含一个排序的(最终反转的)整数列表,这些整数由一些非数字字符分隔 - 例如“10、123、230、750、1034、2003、10101” ;在 B1 我有一个整数 n;我需要一个 不涉及其他单元格 的公式 returns:
- n 如果n属于A1中的列表;
- 否则,如果n不大于A1中的最大值,则A1中的值立即大于n(例如,对于 n = 567,返回值必须为 750);
- 否则报错。
在我看来,解决问题的唯一方法是正则表达式替换(Google Sheet 支持),但直到现在我找不到合理的方法来进行。
有人有(不同的)想法吗?
请尝试:
=if(isnumber(find(B1,A1)),B1,index(split(A1,","),match(B1,split(A1,","),1)+1))
以上不适用于低于第一个的数字,但如果需要可以扩展为:
=if(B1<1*left(A1,find(",",A1)-1),1*left(A1,find(",",A1)-1),if(isnumber(find(B1,A1)),B1,index(split(A1,","),match(B1,split(A1,","),1)+1)))
请尝试:
=index(SORT(TRANSPOSE(SPLIT(A1,", ")),1,0),
MATCH(B1,SORT(TRANSPOSE(SPLIT(A1,", ")),1,0),-1))
在这个公式中我使用了 search_type
= -1 for match function
:
MATCH(search_key, range, search_type)
- search_key - The value to search for. For example, 42, "Cats", or
I24.
range - The one-dimensional array to be searched. If a range with both height and width greater than 1 is used, MATCH will return #N/A!.
search_type - [ OPTIONAL - 1 by default ] - The manner in which to
search.
- 1, the default, causes MATCH to assume that the range is sorted in ascending order and return the largest value less than or equal to
search_key.
- 0 indicates exact match, and is required in situations where range is not sorted.
- -1 causes MATCH to assume that the range is sorted in descending order and return the smallest value greater than or equal to search_key.
简化案例
假设您有一个单元格,其文本按降序排列:
公式为:
=index(SPLIT(A1,", "),MATCH(B1,SPLIT(A1,", "),-1))
比如说,我在 A1 中有一个文本,其中包含一个排序的(最终反转的)整数列表,这些整数由一些非数字字符分隔 - 例如“10、123、230、750、1034、2003、10101” ;在 B1 我有一个整数 n;我需要一个 不涉及其他单元格 的公式 returns:
- n 如果n属于A1中的列表;
- 否则,如果n不大于A1中的最大值,则A1中的值立即大于n(例如,对于 n = 567,返回值必须为 750);
- 否则报错。
在我看来,解决问题的唯一方法是正则表达式替换(Google Sheet 支持),但直到现在我找不到合理的方法来进行。
有人有(不同的)想法吗?
请尝试:
=if(isnumber(find(B1,A1)),B1,index(split(A1,","),match(B1,split(A1,","),1)+1))
以上不适用于低于第一个的数字,但如果需要可以扩展为:
=if(B1<1*left(A1,find(",",A1)-1),1*left(A1,find(",",A1)-1),if(isnumber(find(B1,A1)),B1,index(split(A1,","),match(B1,split(A1,","),1)+1)))
请尝试:
=index(SORT(TRANSPOSE(SPLIT(A1,", ")),1,0),
MATCH(B1,SORT(TRANSPOSE(SPLIT(A1,", ")),1,0),-1))
在这个公式中我使用了 search_type
= -1 for match function
:
MATCH(search_key, range, search_type)
- search_key - The value to search for. For example, 42, "Cats", or I24.
range - The one-dimensional array to be searched. If a range with both height and width greater than 1 is used, MATCH will return #N/A!.
search_type - [ OPTIONAL - 1 by default ] - The manner in which to search.
- 1, the default, causes MATCH to assume that the range is sorted in ascending order and return the largest value less than or equal to
search_key.- 0 indicates exact match, and is required in situations where range is not sorted.
- -1 causes MATCH to assume that the range is sorted in descending order and return the smallest value greater than or equal to search_key.
简化案例
假设您有一个单元格,其文本按降序排列:
公式为:
=index(SPLIT(A1,", "),MATCH(B1,SPLIT(A1,", "),-1))