Copy multiple selections as CSV data to clipboard

Copy multiple selections as CSV data to clipboard

26
Basic UserBasic User
26

    May 02, 2017#1

    I have a simple VB script that I use in Excel that copies selected cell values to the clipboard as a delimited list:

    Code: Select all

    Sub CopyToDelimited()
    
    Dim rRange     As Range
    Dim rCount     As Integer
    Dim pCount     As Integer
    Dim sQuery     As String
    Dim strDelimit As String
    Dim strEnclose As String
    Dim objDataObj As DataObject
    
    Set objDataObj = New DataObject
    
    On Error GoTo Fail
    
    strDelimit = InputBox(Prompt:="Enter character(s) to use as delimiter between each field", _
                           Title:="Delimiter selection  --  defaults to a comma", _
                           Default:=",")
    
    strEnclose = InputBox(Prompt:="Enter character(s) to enclose each field with", _
                           Title:="Enclosure character(s) selection  --  defaults to nothing", _
                           Default:="")
    
    Selection.SpecialCells(xlCellTypeVisible).Select
    rCount = Selection.Count
    pCount = 1
    
    
    For Each rRange In Selection
      If rRange.Text <> "" Then
        If sQuery <> "" Then
          sQuery = sQuery & strEnclose & rRange.Text
        Else
          sQuery = strEnclose & rRange.Text
        End If
         If rCount = pCount Then
          sQuery = sQuery & strEnclose
        Else
          sQuery = sQuery & strEnclose & strDelimit
        End If
      End If
    pCount = pCount + 1
    Next
    
    objDataObj.SetText sQuery, 1
    objDataObj.PutInClipboard
     
    Fail:
    Set objDataObj = Nothing
    End Sub
    It prompts for a delimiter and an encloser character to use.

    I would like to convert this VB to JS so as to be able to use in UEStudio. Does anyone have or know of a VB to JS statement cross reference I could reference?

    Thanks in advance.

    6,675585
    Grand MasterGrand Master
    6,675585

      May 03, 2017#2

      I'm quite sure that there is no such cross reference between Visual Basic and JavaScript as those two script languages have nearly nothing in common.

      The posted VB script is for copying a selection in Microsoft Excel to clipboard formatted as CSV data. But in UEStudio it is not possible to open binary coded Excel files (with exception of hex editing mode). It is only possible to open a CSV file in UEStudio. So selecting a block in an opened CSV and pressing Ctrl+C copies the selection already as CSV formatted data.

      It would be of course possible to write an UE/UES script which copies the selection in active CSV file with a different delimiter and a different enclosing character to clipboard. But that would require a completely different JavaScript code than the VBScript function has because the input data are not Excel cell objects, but is a string block which needs to be parsed and reformatted in memory before copying to clipboard.
      Best regards from an UC/UE/UES for Windows user from Austria

      26
      Basic UserBasic User
      26

        May 03, 2017#3

        Ok, I'm sorry I didn't explain this well. :oops:
        I'm just trying to duplicate the same functionality in a script that I can use in UES.

        For example, Let's say I have these values in UES active window:
        BAXISIFPDMOB
        BAXISIFPD-MOBTLT
        BAXISIPD
        BAXISIPD-MOB
        BAXISITBL-MOBTLT
        BAXISLED-WALL

        The values in red are the ones I have highlighted/selected, I could then run script (copyToDelimted.js) to prompt for delimiter/en-closer character and then place those delimited values in the windows clipboard for pasting wherever I choose.

        Hope that explains it better.

          May 04, 2017#4

          I believe I've figured out one way to accomplish it, but for some reason the UltraEdit.activeDocument.selection only retrieves the last value selected unless I'm using it incorrectly.

          Code: Select all

          UltraEdit.outputWindow.showStatus=false;
          UltraEdit.outputWindow.clear();
          if (UltraEdit.outputWindow.visible == false) UltraEdit.outputWindow.showWindow(true);
          
          selectedValues = UltraEdit.activeDocument.selection;
          UltraEdit.outputWindow.write("Selected Values: " + selectedValues);
          
          
          strDelimit = UltraEdit.getString("Enter character(s) to use as delimiter between each field, defaults to a comma",1);
          if (strDelimit == "") strDelimit = ",";
          
          strEnclose = UltraEdit.getString("Enter character(s) to enclose each field with, defaults to null",1);
          if (strEnclose == "") strEnclose = "";
          
          UltraEdit.outputWindow.write("Delimiter: " + strDelimit);
          UltraEdit.outputWindow.write("Encloser: " + strEnclose);
          For example, If the above code is in an open window and you select on the first line, the first word (double click), then hold down Ctrl key and select (double click) the last word on the first line, the variable selectedValues only contains the last word selected.
          If you do a Ctrl+C and Ctrl+V with the keyboard you will see that both values are in the selection.

          Am I using the UltraEdit.activeDocument.selection correctly?

          6,675585
          Grand MasterGrand Master
          6,675585

            May 04, 2017#5

            Multi-caret and multi-selection handling is not supported by UE/UES macros or scripts.

            But you can get nevertheless what you want by first pressing manually Ctrl+C to copy the multiple selections joined with carriage return and line-feed into active clipboard and then use a code like this one:

            Code: Select all

            // Is there anything in active clipboard?
            if (UltraEdit.clipboardContent.length)
            {
               // Get the strings in active clipboard into an array of strings.
               var asSelectedValues = UltraEdit.clipboardContent.split("\r\n");
            
               // If clipboard contains a line terminator at end, remove
               // the last element from string array being an empty string.
               if (!asSelectedValues[asSelectedValues.length-1].length) asSelectedValues.pop();
            
               // Are there more than 1 string in the array?
               if(asSelectedValues.length > 1)
               {
                  // Join the strings with enclosing them in double quotes
                  // and using a comma as delimiter between the strings
                  // and copy the resulting string to the active clipboard.
                  UltraEdit.clipboardContent = '"' + asSelectedValues.join('","') + '"';
               }
            }
            
            The copy must be done manually. It is not possible to use UltraEdit.activeDocument.copy() in the script as this would result in first discarding all selections with the exception of the last one and second copying just the last remaining selection to the clipboard.
            Best regards from an UC/UE/UES for Windows user from Austria

            26
            Basic UserBasic User
            26

              May 04, 2017#6

              Thanks for your assistance!

              Is there a reason UltraEdit.activeDocument.selection only returns last selected value or is that just the way it is designed to work?

              6,675585
              Grand MasterGrand Master
              6,675585

                May 05, 2017#7

                Yes, there is a reason. Multi-caret and multi-selection handling is not supported by UE/UES macros or scripts.

                You are the first one who I am aware of that have multiple selections made in active file and wants to work with them with a macro or script.

                I'm sure the scripting and perhaps also the macro environment could be enhanced for multiple selections, too. But that would definitely require new functions/commands respectively properties/parameters.

                For example there would be necessary additionally to UltraEdit.activeDocument.selection a property which returns an array of selected strings. The code I posted works only as expected when no selection spans over a line termination.

                There are also needed additional properties for UltraEdit.activeDocument.currentColumnNum and UltraEdit.activeDocument.currentLineNum which return an array of column and line numbers.

                And additional functions/commands are needed for all existing functions/commands moving caret which moves all carets and expand or reduce all selections.

                There should be also additional functions/commands which can be used to add or remove one more caret/selection at a specific line/column.

                The UltraEdit macros and scripts environment is designed mainly for reformatting data in a file. Such interactive usage with actions by user made before or on running a macro/script are not typical and for that reason the UE macro/script environment is currently not really designed for such interactive purposes.
                Best regards from an UC/UE/UES for Windows user from Austria