Forum Discussion

FernandoCG's avatar
FernandoCG
Creator III
11 months ago

Copy to clipboard

Hi, I'm trying to copy data to the clipboard, but, I guess due to limitations, it doesn't finish copying all of it, the chain is cut off.

The idea is, in the tree grid, to make a selection and copy the values ​​of a field determined by me. In practice, it copies the data, but without the selection it is very large and it doesn't copy anything. It copies about 200 characters at most.

Note that the variable I pass to the clipboard does have all the values, but if it is longer than about 200 characters, it does not copy anything to the clipboard.

This is my code:

[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:b137186c-5ab2-46a2-aa65-3ee09070890b:type=javascript&text=if%20%28%21inArgs%20%7C%7C%20%21inArgs.results%20%7C%7C%20%21inArgs.results.getItemByIndex%280%29%29%0A%7B%0A%20%20%20%20return%3B%0A%7D%0A%0Avar%20thisType%20%3D%20this.getType%28%29%3B%0A%09%0Avar%20items%20%3D%20inArgs.results%3B%0Avar%20count%20%3D%20items.getItemCount%28%29%3B%0Avar%20innovator%20%3D%20new%20Innovator%28%29%3B%0Avar%20cadena%20%3D%22%22%3B%0A%0Afor%28var%20i%3D0%3Bi%3Citems.getItemCount%28%29%3Bi%2B%2B%29%7B%0A%20%20%20%20var%20id%20%3D%20items.getItemByIndex%28i%29.getProperty%28%22id%22%2C%22%22%29%3B%0A%20%20%20%20%0A%20%20%20%20var%20thisType%20%3D%20this.getType%28%29%3B%0A%0A%20%20%20%20switch%20%28thisType%29%20%7B%0A%20%20%20%20%20%20%20%20case%20%22MT%20Ruta%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20valorTomado%20%3D%20items.getItemByIndex%28i%29.getProperty%28%22mt_rut_nam%22%2C%22%22%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20case%20%22AIT%20Production%20Order%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20valorTomado%20%3D%20items.getItemByIndex%28i%29.getProperty%28%22ait_po_code%22%2C%22%22%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20case%20%22MT%20ProdRef%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20valorTomado%20%3D%20items.getItemByIndex%28i%29.getProperty%28%22mt_proref_code%22%2C%22%22%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20case%20%22MT%20LineaPVE%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20valorTomado%20%3D%20items.getItemByIndex%28i%29.getProperty%28%22mt_lpve_code%22%2C%22%22%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20case%20%22MT%20Necesidad%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20valorTomado%20%3D%20items.getItemByIndex%28i%29.getProperty%28%22mt_nec_code%22%2C%22%22%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20default%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20L%C3%B3gica%20para%20tipos%20no%20esperados%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20valorTomado%20%3D%20items.getItemByIndex%28i%29.getProperty%28%22keyed_name%22%2C%22%22%29%3B%0A%20%20%20%20%7D%0A%0A%0A%20%20%20%20cadena%20%3D%20cadena%20%2B%20valorTomado%20%2B%22%7C%22%3B%0A%20%20%20%20%0A%7D%0A%20%20%20%20const%20topWindow%20%3D%20aras.getMostTopWindowWithAras%28window%29%3B%20%0A%20%20%20%20if%20%28topWindow.work%20%26%26%20topWindow.work.grid%29%20%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20main.work.searchContainer.runSearch%28%29%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20parent.onRefresh%28%29%3B%0A%20%20%20%20%7D%0A%0Avar%20buffer%20%3D%20cadena.substring%280%2Ccadena.length-1%29%3B%0A%0A%2F%2F%20Not%20all%20browsers%20support%20programmatic%20access%20to%20the%20clipboard%20%28Firefox%29.%0A%2F%2F%20If%20clipboard%20access%20is%20not%20supported%2C%20print%20the%20result%20in%20an%20alert%20%0A%2F%2F%20and%20tell%20the%20user%20to%20copy%20contents%20with%20Ctrl%2BC.%0Aif%20%28aras.utils.isClipboardSupported%28%29%29%0A%7B%0A%20%20%20%20copyToBuffer%28buffer%29%3B%0A%20%20%20%20return%20alert%28%22Item%28s%29%20copiados.%22%29%3B%0A%7D%20%0Aelse%20%0A%7B%0A%20%20%20%20return%20alert%28%22Browser%20does%20not%20allow%20clipboard%20access%3A%5Cn%5Cn%22%20%2B%20buffer%29%3B%0A%7D%0A%0A%2F%2Fnavigator.clipboard.writeText%28cadena.substring%280%2Ccadena.length-1%29%29%3B%0A%2F%2FcopyToBuffer%28cadena.substring%280%2Ccadena.length-1%29%29%3B%0Areturn%3B%0A%0A%0Afunction%20copyToBuffer%28buffer%29%20%7B%0A%20%20%20%20alert%28buffer%29%3B%0A%20%20%20%20if%20%28window.clipboardData%29%20%7B%0A%20%20%20%20%20%20%20%20window.clipboardData.setData%28%27Text%27%2C%20buffer%29%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20aras.utils.setClipboardData%28%27Text%27%2C%20buffer%2C%20window%29%3B%0A%20%20%20%20%7D%0A%7D%20%20%20%20%0A%0A%0A%2F%2A%0Afunction%20copyToBuffer%28buffer%29%20%7B%0A%20%20%20%20alert%28buffer%29%3B%0A%20%20%20%20const%20maxLength%20%3D%20200%3B%20%2F%2F%20Define%20un%20tama%C3%B1o%20m%C3%A1ximo%20por%20iteraci%C3%B3n%0A%20%20%20%20if%20%28window.clipboardData%29%20%7B%0A%20%20%20%20%20%20%20%20alert%281%29%3B%0A%20%20%20%20%20%20%20%20%2F%2F%20Para%20Internet%20Explorer%0A%20%20%20%20%20%20%20%20window.clipboardData.setData%28%27Text%27%2C%20buffer%29%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20alert%282%29%3B%0A%20%20%20%20%20%20%20%20let%20start%20%3D%201%3B%0A%20%20%20%20%20%20%20%20alert%28buffer.length%29%3B%0A%20%20%20%20%20%20%20%20while%20%28start%20%3C%20buffer.length%29%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20const%20part%20%3D%20buffer.substring%28start%2C%20start%20%2B%20maxLength%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20aras.utils.setClipboardData%28%27Text%27%2C%20part%2C%20window%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20start%20%2B%3D%20maxLength%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20alert%28start%29%3B%0A%2F%2F%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20alert%28%22Texto%20copiado%20al%20portapapeles%20en%20partes.%22%29%3B%0A%20%20%20%20%7D%0A%7D%0A%2A%2F%0A]

I have tried several ways, but without any luck.

As a base, I have taken the code from:

https://github.com/ArasLabs/copy-to-clipboard

Any idea??. Too many thanks!!!

3 Replies

  • Ohh the clipboard project! I remember this one! I still use it in a completely different context. 

    Modern Innovator version have integrated the copy&paste clipboard feature. In general everything above I14 has become very copy&paste friendly. 

    I wonder why your content is cut. I did a quick test with following code from the Method editor. I worked fine in my environment. It just throws a long string to the buffer. Does this part fail in your environment?

    [embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:afd8193f-db20-46e2-b13d-e7877e7944d5:type=text&text=const%20buffer%20%3D%20%2212312321908098098888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk000000000000000000000000000000000000000000000000000000000000000zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzjmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm%2C%22%3B%0A%0Aif%20%28window.clipboardData%29%20%7B%0A%20%20%20%20window.clipboardData.setData%28%27Text%27%2C%20buffer%29%3B%0A%7D%20else%20%7B%0A%20%20%20%20aras.utils.setClipboardData%28%27Text%27%2C%20buffer%2C%20window%29%3B%0A%7D]

    • FernandoCG's avatar
      FernandoCG
      Creator III

      Hello, researching and trying and trying, I have realized something curious.

      If I choose 1 or a few records in the grid, the value of the variable is copied to the clipboard, but if many records are chosen, even if the value of the variable is correct, it has the correct data, nothing is copied to the clipboard. That is calculated in the "for", I don't know if it is for speed reasons or something like that.

      This is what ChatGPT tells me:

      "The problem seems to be related to the time it takes for the for loop to execute when there are many items (i.e. getItemCount returns a large number). This can cause the browser to temporarily crash or a problem to occur in the code execution flow due to the accumulation of synchronous operations.

      Also, if you try to copy to the clipboard after a long cycle, some browsers or API functions may fail due to runtime restrictions or memory limitations."

      I could also skip copying the "id"s to the clipboard, for example, and just launch the search automatically, but I don't know how to force a search in the grid directly.

      • AngelaIp's avatar
        AngelaIp
        Catalyst II

        Where do I start....your code is inefficient by design. [emoticon:98f842990f454422aa6a04953955f9d9] It´s like one of my early works from 8 years ago. Don´t worry, it´s not completely wrong, just some aspects don´t work well if you have a huge amount of data. A lot of Innovator code people write just look like this so it´s super normal. But the forum community can do better [emoticon:0e469978eb8947d18b9c41123db3725a] :

        TIP: Chat GPT can also give you many tips how to make things more efficient.

        Following code is not tested and can be wrong, but it collects a few things I noticed. 

        [embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:2af89371-423c-48a2-a510-5cad381f7153:type=javascript&text=%2F%2F%20TIP%201%3A%20Why%20using%20a%20switch%2Fcase%2C%20if%20you%20just%20need%20to%20merge%20an%20static%20ItemType%20name%20with%20a%20static%20property%3F%0A%2F%2F%20Create%20a%20MAP%20to%20match%20ItemType%20names%20to%20target%20properties%20directly%0Aconst%20propertyMap%20%3D%20%7B%0A%20%20%20%20%22MT%20Ruda%22%3A%20%22mt_rut_nam%22%2C%0A%20%20%20%20%22AIT%20Production%20Order%22%3A%20%22ait_po_code%22%2C%0A%20%20%20%20%2F%2F%20Add%20more%20types%20here%0A%7D%3B%0A%0Alet%20content%20%3D%20%5B%5D%3B%0Afor%20%28let%20i%20%3D%200%3B%20i%20%3C%20items.getItemCount%28%29%3B%20i%2B%2B%29%20%7B%0A%20%20%20%20const%20thisType%20%3D%20this.getType%28%29%3B%0A%0A%20%20%20%20%2F%2F%20TIP%202%3A%20Get%20the%20index%20item%20only%20ONCE%20to%20avoid%20repeating%20function%20call%20of%20getItemByIndex%0A%20%20%20%20const%20singleItem%20%3D%20items.getItemByIndex%28i%29%3B%0A%20%20%20%20const%20id%20%3D%20singleItem.getProperty%28%22id%22%2C%20%22%22%29%3B%0A%0A%20%20%20%20%2F%2F%20TIP%203%3A%20Declare%20helper%20variable%20HERE%2C%20outside%20of%20next%20if%20block%2C%20not%20%22inside%22%20like%20you%20have%20done%20in%20the%20%22switch%22.%20In%20your%20case%20you%20declared%20the%20variable%20inside%20the%20switch%20but%20used%20it%20outside%0A%20%20%20%20let%20valorTomado%20%3D%20%22%22%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20Use%20the%20map%20to%20get%20the%20property%20dynamically%0A%20%20%20%20const%20propertyKey%20%3D%20propertyMap%5BthisType%5D%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20Check%20if%20key%20exists%2C%20only%20use%20it%20if%20it%C2%B4s%20there%0A%20%20%20%20if%20%28propertyKey%29%20%7B%0A%20%20%20%20%20%20%20%20valorTomado%20%3D%20singleItem.getProperty%28propertyKey%2C%20%22%22%29%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20if%20no%20entry%20in%20map%20exists%20do%20something%20else%0A%20%20%20%20%20%20%20%20valorTomado%20%3D%20....%0A%20%20%20%20%20%20%20%20%2F%2F%20TIP%204%3A%20Maybe%20throw%20error%20message%20to%20inform%20people%20to%20tell%20the%20admin%20to%20update%20the%20map%0A%20%20%20%20%20%20%20%20aras.AlertError%28%22Couldn%C2%B4t%20get%20value%20for%20%22%20%2B%20thisType%20%2B%20%22.%20Method%20xyz%20needs%20to%20be%20updated.%20Please%20inform%20admin.%22%29%3B%0A%20%20%20%20%7D%0A%20%20%20%20content.push%28valorTomado%20%2B%20%22%7C%22%29%3B%0A%7D%0A%0A%0A%2F%2F%20TIP%205%3A%20Ask%20your%20favourite%20AI%20tool%20about%20efficient%20connectin%20of%20very%20large%20strings.%20E.g.%20instead%20of%20concatenating%20directly%2C%20push%20values%20into%20an%20array%20and%20join%20them%20into%20a%20string%20afterward.%20This%20minimizes%20the%20overhead%20of%20repeatedly%20creating%20new%20strings.%20%0Alet%20cadena%20%3D%20content.join%28%22%22%29%3B]

        I am not sure if it will really help for your use case. For copy&paste from the grid there is also a CSS solution available. I will need to look it up. It´s not perfect, but maybe sufficient for your use case.