简体   繁体   English

PHPExcel从x和y获取单元格

[英]PHPExcel get cell from x and y

I'm making a program and i'm using PHPExcel. 我正在编写一个程序,并且正在使用PHPExcel。 Inside the program you can select width and height. 在程序内部,您可以选择宽度和高度。 (width is the green row, and blue is the height) (宽度是绿色行,蓝色是高度)

All the white numbers are prices. 所有白色数字均为价格。 Lets say i insert 1400 width and 1600 height. 可以说我插入1400宽度和1600高度。 the price should be 640. How can i achieve this? 价格应该是640。我该如何实现?

在此处输入图片说明

The easiest option is to find which column contains 1400 in Row 1 ( D in your example), and which row contains 1600 in column A ( 5 in your example), and then use that to read the value at that cell ( D5 ) 最简单的选择是找到第1行中哪一列包含1400 (在您的示例中为D ),在A列中哪一行包含1600 (在您的示例中为5 ),然后使用该列读取该单元格中的值( D5

A somewhat cleverer way is to use an Excel Formula: 一种更聪明的方法是使用Excel公式:

=INDIRECT(CONCATENATE(SUBSTITUTE(ADDRESS(1,MATCH(1400,A1:E1,0),4),1,""),MATCH(1600,A1:A17,0)))

substituting the appropriate lookup values for 1400 and 1600 , and adjusting the MATCH ranges to the full size for your width and height lookups.... In MS Excel itself, you could use row and column ranges ( A:A ) and ( 1:1 ), but the PHPExcel calculation engine won't handle row and column ranges correctly, but it will work with cell ranges 分别为14001600替换适当的查找值,并根据宽度和高度查找将MATCH范围调整为完整大小。...在MS Excel本身中,可以使用行和列范围( A:A )和( 1:1 ),但是PHPExcel计算引擎无法正确处理行和列范围,但可以使用单元格范围


As an explanation for the above formula: 作为上述公式的解释:

MATCH(1400,A1:E1,0)

Does a lookup for value 1400 in the cell range A1:E1 (looking for a match for your width). 在单元格范围A1:E1中查找值1400(查找与您的宽度匹配)。 The returned value for MATCH() is a numeric offset in that row for the column where it is found. MATCH()的返回值是该行所在行的数字偏移量。 In this case, it will return 4 . 在这种情况下,它将返回4

SUBSTITUTE(ADDRESS(1,<4>,4),1,"")

Converts the column number to a column letter... eg converts 4 to D . 将列号转换为列字母...例如,将4转换为D if your range didn't start at A , then you'd have to add an offset to get the correct letter 如果您的范围不是从A开始,那么您必须添加一个偏移量以获得正确的字母

MATCH(1600,A1:A17,0)

Similar to the previous MATCH, but this time searching in your heights column range ( A1:A17 ) for the value 1600 . 与之前的MATCH相似,但是这次在您的heights列范围( A1:A17 )中搜索值1600 It will return the offset within that range where it finds the value. 它将返回找到该值的范围内的偏移量。 Again, if your range doesn't start in row 1 , then you'd need to add an offset. 同样,如果您的范围不是从第1行开始,那么您需要添加一个偏移量。 In your case, it will return 5 . 在您的情况下,它将返回5

CONCATENATE(<D>,<5>)

Concatenates the calculated column and row values together to give a cell address ( D5 ) 将计算的列和行值连接在一起以给出单元格地址( D5

INDIRECT(<D5>)

Reads the value at the cell address that we've calculated ( D5 ) giving the value 640 读取我们已计算的单元格地址( D5 )上的值,得出值640

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM