I want a simple method (I’m thinking a shape on the Google Sheets worksheet used as a button to execute a Google Script) to, with a single click, copy the contents of the active cell or range (priority on a single cell for now) to the Android (and Windows) clipboard, so the data can be quickly and easily copied and pasted to another app. My primary concern for now is that this work on a mobile device.
I’ve made a button on my Google Sheet, and tried several variations of Google Scripts to accomplish the task. I’ve searched for a solution via Google, DuckDuckGo, StackOverflow, and ChatGPT. So far, all sources except ChatGPT seem to indicate this may not be possible at all, due to Google Scripts running on the server side, and therefore having no access to my local environment, consequently being unable to push anything to my local clipboard.
ChatGPT keeps suggesting a few main tacks that all give errors along the lines of “That isn’t really a command that exists or it is not defined”.
- ClipboardService.getClipboard()
- Utilities.copyToClipboard(value);
- document.execCommand(‘copy’);
#2 seems like it might possibly be a library or add-in that I could link to my script, but I haven’t been able to verify that, and if it is true, I don’t know how/where to find it.
#3 seems to be related specifically to the browser environment itself, and not specific to either Google Sheets or the Windows clipboard environment.
Here are a couple functions I’ve found as suggestions for this problem. The concept and code seem very straightforward, but so far nothing is working.
function copyActiveCellValueToClipboard8() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cell = ss.getActiveCell();
var value = cell.getValue();
Utilities.copyToClipboard(value);
}
function copyToClipboard9() {
var text="Hey it is working!";
Utilities.setClipboard(text);
}
I found a suggestion that a temporary text area could be created, and the cell contents placed in that text area. From there, it could be pushed to the clipboard, and afterwards the text area could be destroyed. This method is failing with the claim that the document.execCommand(‘copy’); command isn’t a real thing. I think the command is for programming Chrome, but isn’t known or available to Google Scripts.
function copyToClipboard7() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var value = sheet.getActiveCell().getValue();
var textarea = document.createElement('textarea');
textarea.value = value;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
SpreadsheetApp.getUi().alert('Copied to clipboard: ' + value);
}
The closest thing I’ve found so far is to have the button launch an HTML sidebar, push whatever data I want copied into a hidden div or some such in the HTML, then have an HTML button in the sidebar that copies the desired data to the clipboard. If I could get that working on a mobile device it would certainly be an acceptable solution, even if it does require two clicks and a sidebar.