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
Create the following code in GAS.
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.
Pass the text you want to copy to the public URL with the text parameter.
Specifically, create and use the following Action from AppSheet.
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])
💪 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.
Here is another one from @Rifad 😀
https://www.googlecloudcommunity.com/gc/Tips-Tricks/Copy-to-clipboard-workaround/m-p/839542#M10264
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.