1

Copy to Clibboard button

Can I create a button to select the contents of a field and copy them to Clipboard? (In FM Pro that was v straightforward)

71 replies

null
    • manuel_cha2
    • 9 mths ago
    • Reported - view

    I want to let you know that the solutions you have presented are exactly what I have been looking for for a long time. However, I have a small problem that I have not been able to solve: I cannot make the text preserve line breaks when copying it. Instead, it gets copied all together, and my desire is to copy it with the line break format. Is this possible?

      • Sean
      • 9 mths ago
      • Reported - view

      I should have asked whether you are using the original version or the Ninext version. I can't get anything after the first line break to copy.

      • gold_cat
      • 9 mths ago
      • Reported - view

       Hi~ Sean,

      I tried to copy the following fields using the original version, but it didn't work. Could you help me check the problem?

      Order has been uploaded
      Order date: January 25, 2024, 16:11
      Order number:HVMHRX5B
      ----------------------------------
      Logistics code:
      9PHGB3CY:02A00108A0039E609A2RPQPE
      
      • Sean
      • 9 mths ago
      • Reported - view

       

      This worked for me. Let me know if it works for you.

      html("
      <button onclick='copyToClipboard(""" +
      replace(YourFieldToBeCopied, "
      ", "\n") +
      """)'>Copy to clipboard</button>
      <script>
      function copyToClipboard(str) {
          var el = document.createElement('textarea');
          el.value = str;
          el.setAttribute('readonly', '');
          el.style.position = 'absolute';
          el.style.left = '-9999px';
          document.body.appendChild(el);
          el.select();
          document.execCommand('copy');
          document.body.removeChild(el);
      }
      </script>")
      

      Replace YourFieldToBeCopied with... your field to be copied!

      • gold_cat
      • 9 mths ago
      • Reported - view

       

      The code is not operating.

      • Sean
      • 9 mths ago
      • Reported - view

       

      I will look at it tonight.

      • gold_cat
      • 9 mths ago
      • Reported - view

       thanks

      • Sean
      • 9 mths ago
      • Reported - view

       

      The code needs to be placed in a Formula field. I don't think JavaScript can be run from a Ninox Button element. I attached a file with two buttons that run the code. One has the default style and the other has a style similar to the Ninox Button element.

      • gold_cat
      • 9 mths ago
      • Reported - view

       

      This solved a long-standing problem, thank you.

      • gold_cat
      • 9 mths ago
      • Reported - view

       hi,sean

      I found that if there are too many carriage returns in the copied field, it cannot be copied. Can this be fixed?

      • Sean
      • 9 mths ago
      • Reported - view

       

      I think it is more likely that there is another white space character like a tab that is causing the problem. Can you upload another database?

      • gold_cat
      • 9 mths ago
      • Reported - view

         

      I'm not sure if you have an Android phone, but this problem is occurring on Android phones. Thanks.

      • Sean
      • 9 mths ago
      • Reported - view

       

      I don't have an Android phone, sorry. On my Mac, everything that is in the Text field is copied to the clipboard. Where is it getting cut off on the Android phones?

      • Sean
      • 9 mths ago
      • Reported - view

      Try the writeText button in the Clipboard API Test formula.

      • gold_cat
      • 9 mths ago
      • Reported - view

       The problem was solved again, thank you!

      • Sean
      • 9 mths ago
      • Reported - view

       You're welcome. I'm glad it worked.

      • manuel_cha2
      • 9 mths ago
      • Reported - view

       

      Hello, thank you for the advice, but for some reason, it didn't work for me. I would like to send you my project, it's quite simple but it would be useful for me to improve my personal productivity.

      I need help reviewing my copy button and making it copy the text with line breaks. When I copy and paste, the text is pasted in a single line. Could you help me with that? Maybe you can solve it in 5 minutes haha, but I'm a beginner and the advice I've received has brought me closer to my goal.

      • Sean
      • 9 mths ago
      • Reported - view

       

      Hello, it took a little longer than 5 minutes, but I got there 😉. Since the source, MSG, is HTML, I put it inside the string function, string(MSG), and that worked.

      Interesting that you used backticks in the formula. I hadn't thought of that when combining Ninox with JavaScript. 😎

      • manuel_cha2
      • 9 mths ago
      • Reported - view

      ¡Wow!  I appreciate the time you have dedicated to this. I'm completely satisfied 100%! Truly, your contribution is extremely valuable to me. Thank you for sharing your knowledge. 😊

    • Sean
    • 9 mths ago
    • Reported - view

    I was very surprised when I first saw that this thread had a "pinned" and "accepted" answer. I haven't seen this anywhere else on the forum. It is more surprising given that execCommand() was deprecated at least 6 months before I posted the first version and that was nearly 3.5 years ago. I didn't discover that it was deprecated until yesterday.

    posted a more modern version in this thread using the Clipboard API, but it is tied to the Ninext project which isn't required to use it. I mean no disrespect towards Jacques and I am still amazed by the discoveries and contributions he has made.

    Even though execCommand() has been deprecated, it is still supported. Even though MDN states that Clipboard API has "Limited availability", it seems to be widely supported. I also figured out some of styling really isn't necessary in the version I posted. Anyway, here is a new version that will use the Clipboard API if it's available and use the execCommand() version if not.

    html("
    <button onclick='pasteToClipboard(""" +
    replace(text, "
    ", "\n") +
    """)'>Paste</button>
    
    <script>
        function pasteToClipboard(str) {
            if (navigator.clipboard) {
                navigator.clipboard.writeText(str)
            } else {
                let textArea = document.createElement('textarea');
                textArea.value = str;
                document.body.appendChild(textArea);
                textArea.select();
                document.execCommand('copy');
                document.body.removeChild(textArea);
            }
        }
    </script>
    ")
    • Silvia.1
    • 8 mths ago
    • Reported - view

    where you can write html code? thank you silvia

      • Sean
      • 8 mths ago
      • Reported - view

       

      You put the code in a Formula field. As far as I know, the code only works in a Form view and not in the Table view.

Content aside

  • Status Answered
  • 1 Likes
  • 8 mths agoLast active
  • 71Replies
  • 7570Views
  • 10 Following