2

私は PHP が初めてで、Excel スプレッドシートからデータを読み取っていて、各行を配列として保存したいと考えています。その配列を取得したら、その配列を別の関数に渡して、各配列値に特定のタイトルを「設定」します。私はこれを理解することができず、誰かが助けてくれることを望んでいました.

最初のスクリプトは次のとおりです。

    <?php 
include './Classes/PHPExcel.php';
include 'testCase.php';

class ExcelReader{

function getExcelFile(){
/*Get file from form*/
    echo 'Hello world' . "\n";
    $FileName = $_FILES["fileName"]["name"];

    /*Move file to server, if file already exists, don't move*/
    if(file_exists("uploadedFiles/".$_FILES["fileName"]["name"])){
        echo $_FILES["fileName"]["name"]." already exists";
    }
    else{
        move_uploaded_file($_FILES["fileName"]["tmp_name"],"uploadedFiles/".$_FILES["fileName"]["name"]);
        echo $_FILES["fileName"]["name"]." has been moved";



    }
    return $FileName;
}

function readExcelFile($FileName){


    /*Create reader object for file and read object in*/
    $PHPExcelReader= PHPExcel_IOFactory::createReaderForFile($FileName);
    $PHPExcelReader->setReadDataOnly(true);
    $PHPExcelObj=$PHPExcelReader->load($FileName);






    $PHPobjWorksheet = $PHPExcelObj->getActiveSheet();

    $topRow = $PHPobjWorksheet->getHighestRow();
    $topCol = $PHPobjWorksheet->getHighestColumn();

    $highestColIndex=  PHPExcel_Cell::columnIndexFromString($topCol);
    for($row=1;$row<=$topRow; $row++){

        $newTestCase = new testCase();
        $testIndex = $newTestCase->getTestCase($row, $highestColIndex, $PHPobjWorksheet);
        echo $testIndex."<- Test Index <br/>";
        $newTestCase->setTestCase($testIndex);
    }
 }
}

$newExcelReader = new ExcelReader;
$newFileName = $newExcelReader->getExcelFile();
$newExcelReader->readExcelFile($newFileName);

?>

これは、データを取得するために使用している2番目のクラスで、設定しようとしています:

    <?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Class for creating test cases to be sent 
 * into TestRail
 * @author Banderson
 */
class testCase {

    /*Function to get the data for each individual test case*/
    function getTestCase($row, $highestColIndex, $PHPobjWorksheet){

            echo "<br />";
            for($col=0;$col<=$highestColIndex;++$col){
                $ii=0;
                $cellValue=array($PHPobjWorksheet->getCellByColumnAndRow($col,$row)->getValue());
                echo $cellValue[$ii]. " | ";
                $ii++;


        }

    return $cellValue;

    }


    /*Function to set the data for each individual test case*/
    function setTestCase($cellValue){
                $title = $cellValue[0];
                echo $title. "<- Title"."<br/>";
                $type = $cellValue[1];
                echo $type. "<- Type"."<br/>";
                $priority = $cellValue[2];
                echo $priority. "<- Priority"."<br/>";
                $estimate = $cellValue[3];
                echo $estimate. "<- Estimate"."<br/>";
                $milestone = $cellValue[4];
                echo $milestone. "<- MileStone"."<br/>";
                $references = $cellValue[5];
                echo $references. "<- 5"."<br/>";
                $preconditions = $cellValue[6];
                echo $preconditions. "<- 6"."<br/>";
                $steps = $cellValue[7];
                echo $steps. "<- 7"."<br/>";
                $expectedResults = $cellValue[8];
                echo $expectedResults. "<- 8"."<br/>";
                $testSuit = $cellValue[9];
                echo $testSuit. "<- 9"."<br/>";


    }



}

?>

そして最後に、私が得ているエラーメッセージは次のとおりです。

    Hello world testrailtestinputV2.xlsx already exists
Title | Type | Priority | Estimate | Milestone | Reference | Preconditions | Steps | Expected Result | Section | Test Suite | | Array<- Test Index<- Title

( ! ) Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 37
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- Type

( ! ) Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 39
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- Priority

( ! ) Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 41
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- Estimate

( ! ) Notice: Undefined offset: 4 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 43
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- MileStone

( ! ) Notice: Undefined offset: 5 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 45
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- 5

( ! ) Notice: Undefined offset: 6 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 47
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- 6

( ! ) Notice: Undefined offset: 7 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 49
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- 7

( ! ) Notice: Undefined offset: 8 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 51
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- 8

( ! ) Notice: Undefined offset: 9 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 53
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- 9

Title 1 | Functionalit | 1 | | | | | 1. Turn phone on 2. Go to main menu 3. Push button | The screen should look like this | | | | Array<- Test Index<- Title

( ! ) Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 37
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- Type

( ! ) Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 39
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- Priority

( ! ) Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 41
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- Estimate

( ! ) Notice: Undefined offset: 4 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 43
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- MileStone

( ! ) Notice: Undefined offset: 5 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 45
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- 5

( ! ) Notice: Undefined offset: 6 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 47
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- 6

( ! ) Notice: Undefined offset: 7 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 49
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- 7

( ! ) Notice: Undefined offset: 8 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 51
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- 8

( ! ) Notice: Undefined offset: 9 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 53
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- 9

Title 2 | Functionalit | 1 | | | | | 1. Turn phone on 2. Go to main menu 3. Push button | The screen should look like this | | | | Array<- Test Index<- Title

どんな助けでも素晴らしいでしょう!前もって感謝します!

4

2 に答える 2

3

関数getTestCaseは、1 つの要素を持つ新しい配列を作成$cellValueし、反復ごとにそれを変数に入れます。したがって、この関数によって返される配列には、要素が 1 つだけ含まれます。次の方法で修正する必要があります。

function getTestCase($row, $highestColIndex, $PHPobjWorksheet) {
  $cellValue = array();
  for($col = 0; $col <= $highestColIndex; ++$col) {
    $v = $PHPobjWorksheet->getCellByColumnAndRow($col,$row)->getValue();
    $cellValue[] = $v; //append element to the array
  }
  return $cellValue;
}
于 2012-05-13T07:56:16.843 に答える
0

行の各セルを個別に読み取ってそれらの値から配列を作成するのではなく、ワークシートの toArray() メソッドを使用して PHPExcel に配列を作成させることができます。

/**
 * Create array from a range of cells
 *
 * @param  string   $pRange             Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
 * @param  mixed    $nullValue           Value returned in the array entry if a cell doesn't exist
 * @param  boolean  $calculateFormulas  Should formulas be calculated?
 * @param  boolean  $formatData         Should formatting be applied to cell values?
 * @param  boolean  $returnCellRef      False - Return a simple array of rows and columns indexed by number counting from zero
 *                                      True - Return rows and columns indexed by their actual row and column IDs
 * @return array
 */
public function rangeToArray($pRange = 'A1', $nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false)

A1:Z1 などの標準の Excel 範囲形式表記を使用して範囲を指定するだけです。

于 2012-05-13T16:52:13.240 に答える