Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I wrote a report to spreadsheet array converter... interested in adding this to the code? #325

Closed
jlvanhulst opened this issue Oct 14, 2023 · 3 comments

Comments

@jlvanhulst
Copy link

jlvanhulst commented Oct 14, 2023

The report API is pretty powerful but the resulting JSON imposing. I could not find an easy processor.
The class below converts the report JSON in an multi dimensional array that could go straight to xls or google sheets with a single line of code.
Is there an interest of adding this to the codebase? (In that case I would have to add a test, which does not seem too hard)

Example:

def getProfitandloss(client):
report = client.get_report('ProfitAndLoss', qs = { "date_macro":'This Fiscal Year-to-date', "accounting_method":'Accrual',"summarize_column_by":'Month'})
return convertReport(report).mainArray

class convertReport:
# Converts a Quickbooks report Json in a spreadsheet like Array
def init(self,report):
self.report = report
self.mainArray = [[]]
self.colTypes = [] # to keep track of the type of each column
self.indent = 0 # indent level for the first column text field
self.header() #create header row and set column types
self.processRows(report["Rows"]["Row"]) #process the rows

def header(self):
    self.header = self.report["Header"]
    self.columns = self.report["Columns"]["Column"]  
    for column in self.columns:
        self.colTypes.append(column["ColType"])
        self.mainArray[0].append(column["ColTitle"])    
    
def cells(self,row):
    # convert a single 'row' of cells into an array, using the column types to convert the values in Floats where type is 'Money'
    # empty money cells are converted to None
    # the first cell of the row is indented by the indent level
    array=[]
    for col in row:
        if (self.colTypes[len(array)] == "Money"):
            if col["value"] == '':
                array.append(None)
            else:
                array.append(float(col["value"]))
        else:
            array.append(col["value"])
    array[0] = " "*self.indent + array[0]
    return array

def processRows(self,row):
    # recursively process the rows of the report
    for r in row:
        if "Header" in r: # process a header row, increment indent
            self.mainArray.append(  self.cells( r["Header"]["ColData"]) )
            self.indent += 1
            
        if "Rows" in r: # this is a row with subrows, process the subrows
            self.processRows(r["Rows"]["Row"])
        elif "ColData" in r: # an actual data row
            self.mainArray.append(  self.cells( r["ColData"]) )
        
        if "Summary" in r: # summary row, decrease indent
            self.mainArray.append( self.cells( r["Summary"]["ColData"]))
            self.indent -= 1  
@jaredthecoder
Copy link
Contributor

@jlvanhulst This is great! I'd be meaning to get around to writing something like this to make QBO's report output more useful. I support adding it in the next release, but obviously that decision is @ej2's.

@ej2
Copy link
Owner

ej2 commented Jan 3, 2024

Report features are lacking in this library. Ultimately I would like to add report objects for all of the QBO reports. Those objects could have alternate ways of accessing the data.

That said, if @jlvanhulst is interested in putting together a PR I will review it.

@jlvanhulst
Copy link
Author

jlvanhulst commented Jan 4, 2024 via email

@ej2 ej2 closed this as completed Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants