You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

求助完善Google Apps Script:实现仅复制单元格值的按钮功能

How to Copy a Cell's Calculated Value to Clipboard via Google Sheets Button

Got it, let's work through this! The main challenge here is that Google Apps Script runs on Google's servers, so it can't directly access your local system clipboard. But we can use a combination of server-side script to fetch the cell value and client-side JavaScript (in a tiny dialog) to handle the clipboard copy. Here's a complete solution:

Step 1: Update Your Script (Code.gs)

Replace your existing script with this. It fetches the cell's calculated value (not the formula) and opens a dialog that copies the value to your clipboard automatically:

function copyCellToClipboard() {
  // Get the active spreadsheet and target sheet
  const thisBook = SpreadsheetApp.getActiveSpreadsheet();
  const thisSheet = thisBook.getSheetByName('Filt_N');
  
  // Get the calculated value from cell A1 (fixes your original typo: getValue() with capital V)
  const cellValue = thisSheet.getRange(1, 1).getValue();
  
  // Create a dialog template and pass the cell value to it
  const htmlTemplate = HtmlService.createTemplateFromFile('CopyDialog');
  htmlTemplate.cellValue = cellValue;
  
  // Render and open the dialog
  const htmlDialog = htmlTemplate.evaluate()
    .setWidth(250)
    .setHeight(100);
  
  SpreadsheetApp.getUi().showModalDialog(htmlDialog, 'Copying Value...');
}

Step 2: Create the Dialog HTML File

In the Apps Script editor, click File > New > HTML file and name it CopyDialog. Paste this code:

<!DOCTYPE html>
<html>
  <body>
    <p>Value copied to clipboard!</p>
    <!-- Hidden input to hold the value for copying -->
    <input type="text" id="hiddenValue" value="<?= cellValue ?>" style="display: none;">
    
    <script>
      // Copy the value to clipboard when the dialog loads
      const hiddenInput = document.getElementById('hiddenValue');
      hiddenInput.select();
      document.execCommand('copy');
      
      // Close the dialog after a short delay for confirmation
      setTimeout(() => {
        google.script.host.close();
      }, 1200);
    </script>
  </body>
</html>

Step 3: Add a Button to Your Sheet

  1. Go back to your Google Sheet.
  2. Click Insert > Drawing and create a button shape or text box to use as your copy trigger.
  3. After drawing, click Save and Close to add it to your sheet.
  4. Right-click the drawing and select Assign script.
  5. Type copyCellToClipboard (the name of our function) and click OK.

How It Works

  • When you click the button, the server-side script pulls the raw calculated value from cell A1 of the Filt_N sheet.
  • It opens a small dialog with a hidden input field containing the value.
  • Client-side JavaScript selects the hidden value and copies it to your clipboard using the browser's native clipboard API.
  • The dialog closes automatically after 1.2 seconds, giving you a quick visual confirmation that the copy worked.

Notes

  • The first time you run the script, you'll need to authorize it (follow the prompts—you may need to click "Advanced" to allow access to your sheet).
  • This copies exactly the value displayed in the cell, not the underlying formula.

内容的提问来源于stack exchange,提问作者AU CH

火山引擎 最新活动