2012-05-13 8 views
2

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

は、最初のスクリプトです:ここでは

<?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); 

?> 

は、私がデータを取得するために使用しています第二のクラスであると私は設定しようとしている:

<?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 

何か助けが素晴らしいでしょう!前もって感謝します!

答えて

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; 
} 
+0

ありがとうございました!それはまさに私が探していたものです!素晴らしい助け!私が投票できる場合、私は:) – BlackHatSamurai

+0

また、ワークシートオブジェクトのtoArray()メソッド –

+0

@Blaine Andersonを使用して、この回答に合格とマークする必要があります。 –

0

のではなく、個別行の各セルを読み、それらの値から自分を配列を構築する、あなたはPHPExcelは、ワークシートののtoArray()メソッドを使用するための配列を作成させることができます

/** 
* 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などの標準のExcel範囲形式の表記を使用して範囲を指定:Z1

関連する問題