Forum Discussion

ThiagoAraujo's avatar
ThiagoAraujo
Occasional Contributor
9 years ago

I write on line 0 excel ?

Hello, a simple and quick questions.
I'm copying a table and inserting her values in excel, though, I'm not able to write the first column of the table.

The variable column starting with the value 1, in which case it writes correctly but does not write the first column.

If I comment out the command to write in excel, and add a log line and put the variable column to 0, you can see the correct columns in the log, including the first.

When trying to write in excel with the variable column to 0, it appears an error screen, and the script does not continue.

It's like in excel not exist 0.

Is it possible to write the first column in some way?

 

var i = 1;
for (i; i < ABody.rows.length; i++)
{
    var linha = ABody.rows.item(i);
    Log.AppendFolder("Row " + i);
    var j = 1; //<- here is the column
    for (j; j < linha.cells.length; j++)
    {
        var cell = linha.cells.item(j);
        Log.Message("Valor " + j + ": " + cell.innerText); //<- Log
	Book.Cells(1) = aqConvert.VarToStr(tag +" --> "+ tag1); //<-Header
	Book.Cells(i, j) = aqConvert.VarToStr(cell.innerText);
	Delay(1000);
     }
}

if I keep the variable 'j' to 1, the code work, but the first column is not written in excel. If I put the variable to 0 an error occurs.

 

This Error:

';' expected
Error location:
Unit: "aneel_coleta\aneel_coleta\Script\test"
Line: 116 Column: 4. <- This Line - Book.Cells(i, j)=aqConvert.VarToStr(cell.innerText);

1 Reply

  • dmiscannon's avatar
    dmiscannon
    Frequent Contributor

    Indexes start at 0 and Excel rows/columns start at 1. To get the code to work, you need to account for this difference. Below is my code in vb script. This works for me. (Sorry, I don't know Jscript.) Hope this helps.

    ******************************************************************************

    Sub Main

     

    dim fp, i, j, Table, RowCount, ColCount, Data, ii, jj

     

    fp = FilePath' This is the file path of your Excel File

    Set FSO = CreateObject("Scripting.FileSystemObject")

    Set objExcel = CreateObject("Excel.Application")

    Set objWorkbook = objExcel.Workbooks.Open(fp)

    Set objSheet = objExcel.ActiveWorkbook.WorkSheets("Sheet1")

    objWorkbook.WorkSheets("Sheet1").Activate

    Set Table = Project.Variables.TestTable ' I created a Table Variable to read from

    RowCount = Table.RowCount

    ColCount = Table.ColumnCount

    For i = 0 to RowCount - 1 ' Row index 1 to rowcount - 1 to account for index starting at 0

      ii = i + 1 ' increase by to account for the difference between the index and Excel numbering for rows

      For j = 0 to ColCount - 1 ' Column index 1 to colcount - 1 to account for index starting at 0

        jj = j+1 ' increase by to account for the difference between the index and Excel numbering for columns

        Data = Table.Item(i,j) ' the data read from the table

        Call Log.Message(Data) ' log the data read

        objWorkbook.Worksheets("Sheet1").cells(jj,ii) = Data ' write the data to the Excel workbook

      Next

    Next

     

    objWorkbook.Save

    objWorkbook.Close

    objExcel.quit

    End Sub