Workaround for Clipboard copy Action

First of all, this workaround is not perfect.
I expect the following features to be implemented

https://www.googlecloudcommunity.com/gc/Feature-Ideas/Copy-to-Clipboard-Action/idc-p/828027

Usage image

2024-11-02_18h36_11.gif

Create the following code in GAS.

2024-11-02_19h08_04.png


Code.gs

 

 

// Code.gs
function doGet(e) {
  // Retrieve the 'text' parameter and pass it to HTML encoded
  const text = e.parameter.text || "";  // Set to an empty string if the parameter is not present
  const template = HtmlService.createTemplateFromFile('index');
  template.text = text;
  return template.evaluate();
}

function getTextToCopy(text) {
  // Decode the newline characters and return
  return decodeURIComponent(text).replace(/\\n/g, "\n");
}

 

 

Index.html

 

 

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <p id="status">Click the text below to copy it to the clipboard.</p>
    <pre id="copyText" style="cursor: pointer; background-color: #f0f0f0; padding: 10px; border-radius: 4px;"></pre>

    <script>
      window.onload = function() {
        const text = "<?= text ?>";  // Get the encoded text passed from the server-side
        if (text) {
          google.script.run.withSuccessHandler(function(decodedText) {
            // Display the retrieved text
            const copyText = document.getElementById('copyText');
            copyText.innerText = decodedText;

            // Copy text to clipboard on click
            copyText.onclick = function() {
              navigator.clipboard.writeText(decodedText)
                .then(() => {
                  document.getElementById('status').innerText = "Copied to clipboard!";
                })
                .catch(err => {
                  console.error('Failed to copy to clipboard: ', err);
                  document.getElementById('status').innerText = "Copy failed.";
                });
            };
          }).getTextToCopy(text);
        } else {
          document.getElementById('status').innerText = "No text found to copy.";
        }
      };
    </script>
  </body>
</html>

 

 

Deploy as a web app.
Set the publication range according to the situation of each application.

2024-11-02_17h00_47.png

Pass the text you want to copy to the public URL with the text parameter.
Specifically, create and use the following Action from AppSheet.

2024-11-02_18h51_14.png

 Target Expression

 

 

"[YOUR PUBLISHED Apps Script UrL]?text="&ENCODEURL([Longtext])

 

 

e.g.)

 

 

"https://script.google.com/a/macros/miyai.jp/s/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/exec?text="&ENCODEURL([Longtext])

 

 

 

15 6 905
6 REPLIES 6

💪 This is a solid workaround right here mate. Nice One

Thank you so much for this tip @takuya_miyai This is a very clever workaround.

Thank you @Fabian_Weller @MultiTech 

I really wanted to have the GAS public URL copied to the clipboard as soon as it was opened, but I still couldn't do it.
(Kind of like doing a phishing scam.)

That's why we need a native Clipboard copy Action.

Thanks, @Fabian_Weller ! I missed this post, but legends have already tried it out. In my code, it’s quite straightforward and even closes the tab on a desktop device. 

btw your url above is not working 


@Rifad wrote:

btw your url above is not working


Thank you. I corrected the link.

Top Labels in this Space