You are very patient and explain very well. Congratulations!!!
@RaghavPal3 жыл бұрын
Thanks Jose
@Gie-nr5wp Жыл бұрын
Hi Raghav, are you also able to show how to write a file (csv, txt or xls) in soapui using groovy? thanks
@RaghavPal Жыл бұрын
I will check on this
@AkhileshAlle6 ай бұрын
Text file is ok. What about the .xlsx file? Does it write into the cells in the Excel sheet?
@RaghavPal6 ай бұрын
Akhilesh You can use Groovy to read and write data to Excel files. Let me guide you through the process. 1. Using Scriptom for Excel Interaction: - Scriptom provides a simple COM library for Groovy, allowing you to work with Microsoft Office tools, including Excel. - It acts as a Groovy wrapper around JACOB (Java COM Bridge), which provides access to COM Automation components. - Here's how you can read and write to Excel files using Scriptom: 2. Dependencies: - First, download the Scriptom JAR file and add it to your classpath. You can find it in the Maven repository [here](www.codelooru.com/2019/02/read-and-write-to-excel-groovy.html). - Import the `ActiveXObject` class in your Groovy script: ```groovy import org.codehaus.groovy.scriptom.ActiveXObject ``` 3. Creating an Excel Object: - Create an instance of `ActiveXObject` for Excel and use it to open an Excel file: ```groovy def excelObj = new ActiveXObject('Excel.Application') def workBook = excelObj.Workbooks.Open('path/to/your/excel-file.xlsx') ``` 4. Accessing Cells: - You can get a reference to a sheet in various ways: - By index: ```groovy def sheetByIndex = workBook.Sheets.Item[1] ``` - As the active sheet: ```groovy def sheetActive = workBook.ActiveSheet ``` - By name: ```groovy def sheetByName = workBook.Sheets('Sheet1') ``` - Access cell data using row and column indices: ```groovy def cellValue = sheet.Cells(2, 1).Value ``` 5. Updating Cell Values: - To update a cell value: ```groovy sheet.Cells(1, 1).Value = 100 ``` 6. Saving Changes: - If you want to save the changes to the workbook: ```groovy workBook.save() ``` - If you don't want to save the changes: ```groovy workBook.Saved = true ``` 7. Getting Row and Column Counts: - Retrieve the row and column counts of the sheet: ```groovy def rows = sheet.UsedRange.Rows.Count def cols = sheet.UsedRange.Columns.Count ``` 8. Closing the Workbook: - Remember to close the workbook after use: ```groovy workBook.close() ``` 9. Working Example: - Here's a sample code snippet that reads data from an active sheet, increments each value by 1, and saves the changes: ```groovy def excelObj = new ActiveXObject('Excel.Application') def workBook = excelObj.Workbooks.Open('path/to/your/excel-file.xlsx') try { def sheet = workBook.ActiveSheet def rows = sheet.UsedRange.Rows.Count def cols = sheet.UsedRange.Columns.Count // Update the values 1.upto(rows, { row -> 1.upto(cols, { col -> sheet.Cells(row, col).Value = sheet.Cells(row, col).Value + 1 }) }) // Print the updated data 1.upto(rows, { row -> 1.upto(cols, { col -> print sheet.Cells(row, col).Value + '\t' }) println '' }) workBook.save() } finally { workBook.close() } ``` Remember to adjust the file path (`'path/to/your/excel-file.xlsx'`) to match your actual Excel file. Happy Excel scripting with Groovy
@janisharma43012 жыл бұрын
sir, In Groovy for any exiting file, how to write on top of the file like as line1. Let's say the file has 100 lines and I need to change the heading or enter a heading at the top of the file. How to do that?
@RaghavPal Жыл бұрын
Hi Jaini, not sure, will need to check online
@vishwagowda8752 жыл бұрын
Hi sir is it possible to replace a line inside a file in this method
@vishwagowda8752 жыл бұрын
I have a file inside that one line has to be edited with new version that should be happen in groovy pipeline how to do that
@RaghavPal2 жыл бұрын
are you not using any version control here
@maheshdeshpande14405 жыл бұрын
very usefull...thank you sir
@RaghavPal5 жыл бұрын
You're welcome Mahesh
@rosemariababu17192 жыл бұрын
could you please share the groovy code to exclude first 4 lines from excel sheet and also need the code to convert excel to xml format?
@RaghavPal2 жыл бұрын
will need to check on that with practical
@rosemariababu17192 жыл бұрын
i am trying this for last one week but not working. could you please help me?
@RaghavPal2 жыл бұрын
will need to check the logs and details
@harshitamittal1752 жыл бұрын
Hi Raghav, i have tried in jenkins scipt... but this data1.txt file is not creating.. but when i am doing println myFile it is showing.. but file is not creating.. i need to create a file. Could you please tell why this file is not creating
@RaghavPal2 жыл бұрын
Hi Harshita, will need to check the logs. Pls try all the steps again
@arunkumararjunan86085 жыл бұрын
myFile.renameTo("test.txt") works fine.. what is the need for new File("test.txt") here?
@RaghavPal5 жыл бұрын
Hi Arun, showing diff ways, see what works best for you.
@poornendra5112 жыл бұрын
I have to create a file using file function def file= new File('sample.txt') File = file.replaceAll('first', sample) After replace I want to create new file how can I use file function to create new file
@RaghavPal2 жыл бұрын
Hi, this can help stackoverflow.com/questions/48566158/how-to-create-text-file-using-groovy
@petunialeboho34962 жыл бұрын
how to do a find and replace word in a file Thanks
@RaghavPal2 жыл бұрын
Hi Petunia, this can help stackoverflow.com/questions/17395787/how-to-replace-a-string-word-in-a-text-file-in-groovy/29393157
@petunialeboho34962 жыл бұрын
@@RaghavPal thank you 🙏
@poornapoorna11452 жыл бұрын
Hi Raghava i was tring to create a file in server using in jenkins pipeline its was getting executed but file is not getting created in server def content = new File("/home/devops /ANSIBLE/yamlsamples/first.yaml") def filedata = new File(content).getText() writeFile file:"/home/devops /ANSIBLE/yamlsamples/second.yaml" , text: filedata
@RaghavPal2 жыл бұрын
Hi Poorna, I am not too sure on this, will need to check some examples online