Hi there fellow developers,
I'm trying to export a large amount of data to Excel using Progress 4GL. We're talking about ±5500 records and ±170 columns.
The problem is that this is too much for Excel to handle and it raises the following error: Excel error.
I can continue and it exports all the data, but the procedure isn't exactly presentable with an error right in the middle of it.
I'm using the following code to export all data from different CSV files into one single Excel file with a seperate worksheet for each CSV file:
/* Initialize Excel file & add new workbook */
CREATE "Excel.Application" vchExcel.
vchExcel:SheetsInNewWorkbook = NUM-ENTRIES(ipcSheets,',').
vchExcel:WorkBooks:ADD().
ASSIGN vchWorkBook = vchExcel:WorkBooks:Item(1).
/* Import each file's data into a new sheet of the workbook */
Sheet:
DO iTab = 1 TO NUM-ENTRIES(ipcFiles) ON ERROR UNDO, RETRY Sheet:
/* Import CSV data into Excel */
ASSIGN cConnection = SUBSTITUTE("TEXT;" + gvcExportPath + "csv_files\" + "&1",ENTRY(iTab,ipcFiles))
vchWorkSheet = vchExcel:Sheets:ITEM(iTab)
vchWorkSheet:NAME = ENTRY(iTab,ipcSheets)
lResult = vchWorkSheet:QueryTables:ADD(cConnection,vchWorkSheet:cells(1,1)).
ASSIGN
vchQueryTable = vchWorkSheet:QueryTables(1)
vchQueryTable:FieldNames = TRUE
vchQueryTable:RowNumbers = FALSE
vchQueryTable:FillAdjacentFormulas = FALSE
vchQueryTable:PreserveFormatting = FALSE
vchQueryTable:RefreshOnFileOpen = FALSE
vchQueryTable:RefreshStyle = 1
vchQueryTable:SavePassword = FALSE
vchQueryTable:SaveData = TRUE
vchQueryTable:AdjustColumnWidth = TRUE
vchQueryTable:RefreshPeriod = 0
vchQueryTable:TextFilePromptOnRefresh = FALSE
vchQueryTable:TextFilePlatform = 437
vchQueryTable:TextFileStartRow = 1
vchQueryTable:TextFileParseType = 1
vchQueryTable:TextFileTextQualifier = 1
vchQueryTable:TextFileConsecutiveDelimiter = TRUE
vchQueryTable:TextFileTabDelimiter = TRUE
vchQueryTable:TextFileSemicolonDelimiter = TRUE
vchQueryTable:TextFileCommaDelimiter = FALSE
vchQueryTable:TextFileSpaceDelimiter = FALSE
vchQueryTable:TextFileTrailingMinusNumbers = TRUE
lResult = vchQueryTable:REFRESH
vchQueryTable:BackgroundQuery = FALSE.
/* Catch all errors */
CATCH eAnyError AS Progress.Lang.ERROR:
RUN disp_mesg(INPUT SUBSTITUTE("ERROR! -> &1",eAnyError:GetMessage(1))).
RUN adnew_log(INPUT SUBSTITUTE("ERROR OCCURRED!")).
RUN adnew_log(INPUT SUBSTITUTE("=> &1: &2",eAnyError:GetMessageNum(1),eAnyError:GetMessage(1))).
END CATCH.
END.
/* Save & close */
vchExcel:Visible = FALSE.
vchExcel:DisplayAlerts = FALSE.
vchWorkBook:SaveAs(gvcExportPath + ipcName + '.xlsx',,,,,,).
vchWorkBook:CLOSE().
/* Release All Objects */
RELEASE OBJECT vchQueryTable NO-ERROR.
RELEASE OBJECT vchWorkSheet NO-ERROR.
RELEASE OBJECT vchWorkBook NO-ERROR.
vchExcel:QUIT(). /* Quit Excel */
RELEASE OBJECT vchExcel NO-ERROR.
Does any of you know how to solve this issue and get the desired result? Thanks in advance!
Why controlling XL from Progress?
If XL is a mandatory step... I would have rather do it the way around... call XL with a macro-file to open the CSVs and save in XL format.
If this needs to be integrated into a business process... I would just open XL with the macro-file right after the CSVs generation.
Related
I am trying to export some text from annotation fields in Nuance Power PDF to Excel using VBA. I added the Nuance Power PDF reference to Excel VBA (PDF Plus).
I used it and it works well but the text returned from fields is empty.
Set PDFApp = CreateObject("NuancePDF.App")
Set dvDoc = CreateObject("NuancePDF.DVDoc")
dvDoc.Open("\\adpdc-2\Users$\a.goudinoux\Documents\Macro Formulaire\fiche.pdf")
Set ddDoc = dvDoc.GetDDDoc()
Set ddPage = ddDoc.AcquirePage(0)
nbannots = ddPage.GetNumAnnots() - 1
For i = 0 To nbannots
Texte = ""
Set ddAnnot = ddPage.GetAnnot(i)
Set ddText = ddDoc.CreateTextSelect(0, ddAnnot.GetRect())
ThisWorkbook.Sheets(1).Cells(1, i) = ddAnnot.GetTitle()
For k = 0 To ddText.GetNumText()
Texte = Texte & ddText.GetText(k)
Next
ThisWorkbook.Sheets(1).Cells(2, i) = Texte
Next
Part of the PDF document :
Results :
As you can see the first line is working but not the second one.
I thought the problem was with ddText but ddText.GetNumText() gives the right number of text elements in the text selection (ex : 2, 5, 4, etc...) when I run my program in Debug Mode.
I think the problem is from the function GetText(k).
I made it work once but I can't find my code back..
Do you see any mistake ?
Either your annotations are more complex than indicated or you're overthinking it.
Just read the annotation with GetContents and call it a day.
Set PDFApp = CreateObject("NuancePDF.App")
Set dvDoc = CreateObject("NuancePDF.DVDoc")
dvDoc.Open("\\adpdc-2\Users$\a.goudinoux\Documents\Macro Formulaire\fiche.pdf")
Set ddDoc = dvDoc.GetDDDoc()
Set ddPage = ddDoc.AcquirePage(0)
nbannots = ddPage.GetNumAnnots() - 1
For i = 0 To nbannots
Set ddAnnot = ddPage.GetAnnot(i)
ThisWorkbook.Sheets(1).Cells(1, i) = ddAnnot.GetTitle
ThisWorkbook.Sheets(1).Cells(2, i) = ddAnnot.GetContents
Next i
I'm trying to add a calculated column into this pivot table that is being generated via a python script with import win32com.client.
Code is posted below that is generating the pivot table. I'm not sure how to add a column. In VBA it would be wb.PivotTable.CalculatedField.Add but that didn't work (at least the syntax I tried).
I'm trying to calculate [OoCUnits] / [GrossUnits]
#Make Pivot version with four weeks data
Excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
win32c = win32com.client.constants
wb = Excel.Workbooks.Open(filename)
ws3 = wb.Worksheets('Dataset')
cl1 = ws3.Cells(1,1)
cl2 = ws3.Cells(max_row,max_col)
PivotSourceRange = ws3.Range(cl1,cl2)
ws3.Activate()
PivotSourceRange.Select()
wb.Sheets.Add (After=wb.Sheets(3))
ws4 = wb.Worksheets(4)
ws4.Name = 'Pivot'
cl3 = ws4.Cells(4,1)
PivotTargetRange = ws4.Range(cl3,cl3)
PivotTableName = 'OoCPivot'
#Make Pivot Table
PivotCache = wb.PivotCaches().Create(SourceType=win32c.xlDatabase, SourceData=PivotSourceRange, Version=win32c.xlPivotTableVersion14)
PivotTable = PivotCache.CreatePivotTable(TableDestination=PivotTargetRange, TableName=PivotTableName, DefaultVersion=win32c.xlPivotTableVersion14)
PivotTable.PivotFields('Product Name').Orientation = win32c.xlRowField
PivotTable.PivotFields('Product Name').Position = 1
PivotTable.PivotFields('Customer Number').Orientation = win32c.xlPageField
PivotTable.PivotFields('Customer Number').Position = 1
PivotTable.PivotFields('Customer Name').Orientation = win32c.xlPageField
PivotTable.PivotFields('Customer Name').Position = 2
PivotTable.PivotFields('Week Ending Date').Orientation = win32c.xlColumnField
PivotTable.PivotFields('Week Ending Date').Position = 1
DataField = PivotTable.AddDataField(PivotTable.PivotFields('GrossUnits'))
DataField.NumberFormat = '#0.00'
DataField = PivotTable.AddDataField(PivotTable.PivotFields('OoCUnits'))
DataField.NumberFormat = '#0.00'
I'm inserting the values with the DataField. The script is not presenting any issues as is.
EDIT: The exact code I implemented to solve. The last two lines are just formatting. I'm including in case it helps someone else.
CalcField = PivotTable.CalculatedFields().Add('OoC Unit %','= OoCUnits / GrossUnits')
DataField = PivotTable.AddDataField(PivotTable.PivotFields('OoC Unit %'))
DataField.NumberFormat = '#%'
PivotTable.DisplayErrorString = True
Actually, the method is PivotTable.CalculatedFields().Add() according to docs. Consider placing below at bottom:
CalcField = PivotTable.CalculatedFields().Add("OC_GrossPct", "= OoCUnits / GrossUnits")
PivotTable.PivotFields("OC_GrossPct").Orientation = win32c.xlDataField
I have an issue with creating .txt load files for SAP LSMW. The code takes data from an opened workbook and then populates various .txt files by various conditions - mostly by headers. The file worked well, but I would like to move data into two separate load files based on the data that is in the column under "BUKRS". Here comes an issue with Hlookup not being able to look for various values. Moreover, all of the data gets saved only to the file "05-Vend-Loadcache(No WHTAX).xls" I woudl like to divide the files by cost centre numbers that are in column "BUKRS". These are 9000, 5500, 6200, 8400, 8600, 8500. These ones should go to "06-Vend-Loadcache(WHTAX).xls". Unfortunately it does not work and only "05-Vend-Loadcache(No WHTAX).xls" gets populated. I have no clue why? Could you help me, please?
Sub VENDOR()
'CC3200 (LSMW1) LNRZB (LSMW4) Line 60 for 5500 ....
CCD = Application.WorksheetFunction.HLookup("BUKRS", DTA.Range("A1:IV2"), 2, 0)
CCE = DTA.Cells(1, 60)
WFNA = "05-Vend-Loadcache(No WHTAX).xls"
Select Case CCD
Case 9000, 5500, 6200, 8400, 8600, 8500
WFNA = "06-Vend-Loadcache(WHTAX).xls"
End Select
SHT = "BISOVSH"
DATACHK = 1
If ((CCE = "" Or CCE = "CC3200" Or CCE = "VERKF" Or CCE = "TELF1" Or CCE = "KZRET") And WFNA = "05-Vend-Loadcache(No WHTAX).xls") Then
Call EXPORTDTA
Else
'do nothing as not to Export
End If
If CCE = "LNRZB" And WFNA = "06-Vend-Loadcache(WHTAX).xls" Then
Call EXPORTDTA
Else
'do nothing as not to Export
End If
End Sub
The following code helps to import a sales line to an invoice:
Dim LineItems(dtItem.Rows.Count) As taSopLineIvcInsert_ItemsTaSopLineIvcInsert
Dim salesLine As New taSopLineIvcInsert_ItemsTaSopLineIvcInsert
'Create Invoice Sales lines
For Each dr In dtItem.Rows
With salesLine
.CUSTNMBR = dr.Item("acctno")
.SOPNUMBE = invoiceNumber
.SOPTYPE = 3
.DOCID = "STD INV"
.QUANTITY = dr.Item("Qty")
.ITEMNMBR = dr.Item("Item")
.ITEMDESC = dr.Item("Memo")
.UNITPRCE = dr.Item("SalesPrice")
.XTNDPRCE = dr.Item("Credit")
.TAXAMNT = 0
.UOFM = "Each"
.SALSTERR = "GENERAL"
.ReqShipDate = dtHdr.Rows(0).Item("InvoiceDate").ToString()
.FUFILDAT = dr.Item("Date1").ToString()
.ACTLSHIP = dr.Item("Date1").ToString()
'.NONINVEN = 0
.DOCDATE = dtHdr.Rows(0).Item("InvoiceDate").ToString()
.SLPRSNID = "C1"
End With
LineItems(rowCtr) = salesLine
rowCtr = rowCtr + 1
Next
The fields for SLPRSNID & SALSTERR are just ignored. The invoice itself is being created with all the line items. ANY ideas from anyone with experience using this API are appreciated!
Since we are not integrating SOP commissions data, importing SLPRSNID & SALSTERR on line items using code like above is not supported. However, eConnect does expose pre and post stored procedures to customize business logic. The following was added in taSopHdrIvcInsertPost to implement the import:
/** Custom Business Logic */
if ((#I_vSLPRSNID <> '') and (#I_vSALSTERR <> ''))
begin
update SOP10200
set SLPRSNID = #I_vSLPRSNID, SALSTERR = #I_vSALSTERR
where SOPNUMBE = #I_vSOPNUMBE and SOPTYPE = #I_vSOPTYPE
end
/** Custom Business Logic */
NOTE: with this implementation, the specific SLPRSNID & SALSTERR will have to be provided to the line items, it will not automatically post from header data, or any customer setup data in GP - your integration has to pass the values in.
I want to create excel files in Groovy, then have them plotted. This code was taken from an example using Microsoft's Shell Scripting language:
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Cells(1,1) = "Operating System"
objWorksheet.Cells(2,1) = "Windows Server 2003"
objWorksheet.Cells(3,1) = "Windows XP"
objWorksheet.Cells(5,1) = "Windows NT 4.0"
objWorksheet.Cells(6,1) = "Other"
objWorksheet.Cells(1,2) = "Number of Computers"
objWorksheet.Cells(2,2) = 145
objWorksheet.Cells(3,2) = 987
objWorksheet.Cells(4,2) = 611
objWorksheet.Cells(5,2) = 41
objWorksheet.Cells(6,2) = 56
Set objRange = objWorksheet.UsedRange
objRange.Select
Set colCharts = objExcel.Charts
colCharts.Add()
Set objChart = colCharts(1)
objChart.Activate
objChart.HasLegend = FALSE
objChart.ChartTitle.Text = "Operating System Use"
How would I modify this to work in Groovy?
You need groovy to work with COM. Towards the bottom of this page is an example of automating Excel.
EDITS
Here's your example translated into Groovy (I ran this under Groovy 1.8.2):
import org.codehaus.groovy.scriptom.*
import org.codehaus.groovy.scriptom.tlb.office.excel.XlChartType
import org.codehaus.groovy.scriptom.tlb.office.excel.XlRowCol
import org.codehaus.groovy.scriptom.tlb.office.excel.XlChartLocation
// create a xls instance
def xls = new ActiveXObject("Excel.Application")
xls.Visible = true
Thread.sleep(1000)
// get the workbooks object
def workbooks = xls.Workbooks
// add a new workbook
def workbook = workbooks.Add()
// select the active sheet
def sheet = workbook.ActiveSheet
cell = sheet.Range("A1")
cell.Value = "Operating System"
cell = sheet.Range("A2")
cell.Value = "Windows Server 2003"
cell = sheet.Range("A3")
cell.Value = "Windows XP"
cell = sheet.Range("A4")
cell.Value = "Windows NT 4.0"
cell = sheet.Range("A5")
cell.Value = "Other"
cell = sheet.Range("B1")
cell.Value = "Number of Computers"
cell = sheet.Range("B2")
cell.Value = 145
cell = sheet.Range("B3")
cell.Value = 987
cell = sheet.Range("B4")
cell.Value = 611
cell = sheet.Range("B5")
cell.Value = 41
def chart = workbook.Charts.Add(Scriptom.MISSING, sheet) // create chart object
chart.ChartType = XlChartType.xl3DArea // set type to pie
chart.SetSourceData(sheet.Range("A1:B5"), XlRowCol.xlColumns) // set source data
chart.Location(XlChartLocation.xlLocationAsNewSheet) // add chart as new sheet
Mark's answer above was a great example. It's event easier to follow if you make a couple simple changes:
import org.codehaus.groovy.scriptom.*
import org.codehaus.groovy.scriptom.tlb.office.excel.*
def xls = new ActiveXObject("Excel.Application")
xls.Visible = true
Thread.sleep(1000)
// add a new workbook
def workbook = xls.Workbooks.Add()
// select the active sheet
def sheet = workbook.ActiveSheet
sheet.Range("A1").Value = "Operating System"
sheet.Range("A2").Value = "Windows Server 2003"
sheet.Range("A3").Value = "Windows XP"
sheet.Range("A4").Value = "Windows NT 4.0"
sheet.Range("A5").Value = "Other"
sheet.Range("B1").Value = "Number of Computers"
sheet.Range("B2").Value = 145
sheet.Range("B3").Value = 987
sheet.Range("B4").Value = 611
sheet.Range("B5").Value = 41
def chart = workbook.Charts.Add(Scriptom.MISSING, sheet) // create chart object
chart.ChartType = XlChartType.xl3DArea // set type to pie
chart.SetSourceData(sheet.Range("A1:B5"), XlRowCol.xlColumns) // set source data
chart.Location(XlChartLocation.xlLocationAsNewSheet) // add chart as new sheet
Another option may be Apache POI, depending on what all you need to actually implement.
Either option will be complicated for a beginner; the easy path would be to use the shell.
I would also go for Apache POI. A working example can be found at the busy developer guide. There is also GSheets, a thin groovy wrapper for Apache POI, see the blog post and the unit test for an example usage.
Probably simplest (in terms of external code) is text format, cells are delimited by '\t' and lines "\r\n".
Be careful on cell having sense String but with num characters (or Date-alike), best to prepend with single apostrophe
Such format can be pasted by clipboard or open from file menu.