Anything to add up selected numbers? (or plugin suggestion)



  • Let’s say I have either of these two (attahced) sets of numbers in my text file
    numbers01.jpg numbers 02.jpg
    There’s a site out there, calculla.com, where you can paste in numbers like this, and it will add them up and give you a total sum. That’s whether the numbers are vertical or horizontal. Considers spaces between numbers, and/or new lines, to be delimiters which separates each number.
    Is there any plugin out there that will do anything like that?
    I know of another text editor (one which stinks in every other respect compared to NPP), but it does have a function to add up selected numbers.
    Anyone know of a plugin, or a way to do this?
    Thanks!



  • @Chris-Tanguay-0

    In 32-bit Notepad++, there is a plugin called NppCalc, but AFAICT, it requires an actual expression rather than just adding any numbers in the document.

    There is a newer plugin I’ve never seen before called Expression Calculator (listed in both 32bit and 64bit NPP), but I know nothing about that one, and the homepage it directs you to says nothing about a Notepad++ plugin… There, I was able to find the download link and thus extrapolate to the github home for Expression Calculator, which shows that it appears to be very similar to the NppCalc, and thus probably not exactly what you want.

    If I wanted to add up all the numbers in a document, I would pull out the PythonScript plugin and write a script to do it, something like the script shown at the end of this post. (It contains the instructions for how to install PythonScript, how to add this script to PythonScript and run it, and how to add a keyboard shortcut if desired)

    That script will take the input

    3 14 15
    9 27
    2 7 18 28
    18 28
    

    and will become

    3 14 15
    9 27
    2 7 18 28
    18 28
    
    ====TOTAL=169====
    

    ----------

    # encoding=utf-8
    """in response to https://community.notepad-plus-plus.org/topic/21653/
    
    Add up all the numerical tokens in a file, separated by whitespace.
    It will append the total to the end of file, like
    
        =====TOTAL=###=====
    
    If you don't want just unsigned integers, you'll have to change the search
    regular expression and change the code in the function to not force
    integer arithmetic.
    
    ----
    
    this uses the "add_1" style search in Notepad++
    
    INSTRUCTIONS:
    1. Install PythonScript
       * Plugins > Plugins Admin
       * Click ☑ PythonScript checkbox until checked
       * click INSTALL
       * restart Notepad++ as necessary
    2. Create new PythonScript script
       * Plugins > PythonScript > New Script
       * Give it a reasonable name, like AddAllInts.py
       * Paste in the code from this black box
       * Save
    3. Switch to the document where you want to search/replace
    4. Plugins > PythonScript > Scripts > AddAllInts
    
    To assign a keyboard shortcut:
    1. Plugins > PythonScript > Configuration
    2. In the User Scripts, click on AddAllInts.py
    3. Click the left ADD button (above Menu Items)
        * This adds "AddAllInts" to the main Plugins > PythonScript menu, rather
          than requiring you to dig down into the Scripts submenu
    4. Exit Notepad++ and restart
        * This is required for ShortcutMapper to be able to see the new entry
          in the PythonScript menu
    5. Settings > ShortcutMapper > Plugin Commands
    6. Filter by AddAllInts
    7. Click on AddAllInts, then click the MODIFY button and
        set the keyboard shortcut in the dialog
    
    PythonScript has documentation in its Context-Sensitive Help
    """
    from Npp import *
    
    grand_total = 0
    
    def add_int_to_total(m):
        global grand_total
        i = int(m.group())
        grand_total += i
    
    editor.beginUndoAction()
    editor.documentStart()
    editor.research(r'\b\d+\b', add_int_to_total)
    editor.documentEnd()
    editor.addText("\n====TOTAL={}====\n".format(grand_total))
    editor.endUndoAction()
    


  • Hello, @Chris-Tanguay-0, @peterjones and All,

    An other solution, less elegant of course, would be to use, first, a regex S/R, then the calc.exe utility !

    For instance, given the INPUT text :

    Any_text3 14 15
    9 27
    2 7 18 28
    18 28any_other_text
    
    • Then the following regex S/R :

      • SEARCH (\d+)\s*(?=(\d+)|.|\z)

      • REPLACE $1?2+:=

    Would be changed as the OUTPUT text, below :

    Any_text3+14+15+9+27+2+7+18+28+18+28=any_other_text
    
    • Then, select the numeric part 3+14+15+9+27+2+7+18+28+18+28=

    • Hit the Ctrl + C shortcut

    • Hit the Win Key + R shortcut

    • Type in calc and valid with Enter

    • Hit the Ctrl + V shortcut

    => Result = 169

    Best Regards,

    guy038

    And the text, with tabulation chars, between the two columns :

    10	8
    6	10
    1	3
    4	3
    

    Would give :

    10+8+6+10+1+3+4+3=
    

    => Result = 45



  • This post is deleted!

Log in to reply