You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Sheets脚本开发:实现打开时自动缩放至75%及修复UserProperties问题

Solution: Automatic 75% Zoom + Fix Deprecated UserProperties for Shared Google Sheets

Got it, let's break down how to solve both your needs: adding automatic 75% zoom when the spreadsheet opens, and updating your last-edit tracking script to replace the deprecated UserProperties API.

1. Implement Automatic 75% Zoom on Open

Google Apps Script's server-side APIs can't directly control the spreadsheet's zoom level (that's a client-side browser UI setting). Instead, we'll use client-side JavaScript injected via HtmlService to adjust the zoom when the sheet loads.

Here's how to add this to your onOpen function:

function onOpen() {
  // Restore last edited sheet and cell (we'll update this part next)
  const userProps = PropertiesService.getUserProperties();
  const lastModifiedSheet = userProps.getProperty("mySheetName");
  const lastModifiedCell = userProps.getProperty("myCell");

  if (lastModifiedSheet && lastModifiedCell) {
    try {
      SpreadsheetApp.getActiveSpreadsheet()
        .getSheetByName(lastModifiedSheet)
        .getRange(lastModifiedCell)
        .activate();
    } catch (e) {
      // Fallback if the sheet/cell no longer exists
      console.log("Couldn't activate last edited location: " + e.message);
    }
  }

  // Inject client-side script to set zoom to 75%
  const zoomHtml = HtmlService.createHtmlOutput(`
    <script>
      window.addEventListener('load', () => {
        // Target Google Sheets' zoom slider control
        const zoomSlider = document.querySelector('.docs-zoom-controls input[type="range"]');
        if (zoomSlider) {
          // Set slider value to 75 (matches 75% zoom)
          zoomSlider.value = 75;
          // Trigger change event to apply the zoom
          zoomSlider.dispatchEvent(new Event('change', { bubbles: true }));
        }
        // Close the tiny sidebar immediately after running
        setTimeout(() => {
          google.script.host.close();
        }, 100);
      });
    </script>
  `).setWidth(1).setHeight(1); // Make the sidebar invisible

  SpreadsheetApp.getUi().showSidebar(zoomHtml);
}

Notes on Zoom Functionality:

  • This relies on Google Sheets' current DOM structure. If Google updates their UI, this might need a small tweak, but it works for the current version.
  • The sidebar is made nearly invisible and closes itself right after running, so users won't notice it.

2. Fix Deprecated UserProperties

UserProperties was replaced with PropertiesService.getUserProperties() in newer Google Apps Script versions. Here's how to update your tracking functions:

Updated Trigger Setup (unchanged, but for context):

function setTrigger() { 
  const ss = SpreadsheetApp.getActive(); 
  ScriptApp.newTrigger("trackLastEdit")
    .forSpreadsheet(ss)
    .onEdit()
    .create(); 
}

Updated Last-Edit Tracking Function:

function trackLastEdit() { 
  const sheet = SpreadsheetApp.getActiveSheet(); 
  const sName = sheet.getName(); 
  const currentCell = sheet.getActiveCell().getA1Notation(); 

  // Use PropertiesService instead of deprecated UserProperties
  const userProps = PropertiesService.getUserProperties();
  userProps.setProperty("mySheetName", sName);
  userProps.setProperty("myCell", currentCell);

  // Removed duplicate setProperty call and unnecessary sleep delays
}

Key Changes:

  • Replaced all UserProperties calls with PropertiesService.getUserProperties().
  • Removed duplicate setProperty("mySheetName", sName) and unnecessary Utilities.sleep() calls (they weren't needed here).
  • Renamed myFunction to trackLastEdit for better readability.

Full Combined Script

Here's the complete script with both features integrated:

// Install this trigger once to enable edit tracking
function setTrigger() { 
  const ss = SpreadsheetApp.getActive(); 
  ScriptApp.newTrigger("trackLastEdit")
    .forSpreadsheet(ss)
    .onEdit()
    .create(); 
}

// Tracks the last edited sheet and cell for the current user
function trackLastEdit() { 
  const sheet = SpreadsheetApp.getActiveSheet(); 
  const sName = sheet.getName(); 
  const currentCell = sheet.getActiveCell().getA1Notation(); 

  const userProps = PropertiesService.getUserProperties();
  userProps.setProperty("mySheetName", sName);
  userProps.setProperty("myCell", currentCell);
}

// Runs when the spreadsheet opens: restores last edit location + sets 75% zoom
function onOpen() {
  // Restore last edited position
  const userProps = PropertiesService.getUserProperties();
  const lastModifiedSheet = userProps.getProperty("mySheetName");
  const lastModifiedCell = userProps.getProperty("myCell");

  if (lastModifiedSheet && lastModifiedCell) {
    try {
      SpreadsheetApp.getActiveSpreadsheet()
        .getSheetByName(lastModifiedSheet)
        .getRange(lastModifiedCell)
        .activate();
    } catch (e) {
      console.log("Fallback: Couldn't access last edited location - " + e.message);
    }
  }

  // Set zoom to 75% via client-side script
  const zoomHtml = HtmlService.createHtmlOutput(`
    <script>
      window.addEventListener('load', () => {
        const zoomSlider = document.querySelector('.docs-zoom-controls input[type="range"]');
        if (zoomSlider) {
          zoomSlider.value = 75;
          zoomSlider.dispatchEvent(new Event('change', { bubbles: true }));
        }
        setTimeout(() => google.script.host.close(), 100);
      });
    </script>
  `).setWidth(1).setHeight(1);

  SpreadsheetApp.getUi().showSidebar(zoomHtml);
}

How to Use:

  1. Open your Google Sheet, go to Extensions > Apps Script.
  2. Replace the existing code with the full script above.
  3. Run the setTrigger function once (you'll need to authorize the script when prompted).
  4. Close and reopen the sheet to test the zoom and last-edit restore features.

内容的提问来源于stack exchange,提问作者Mark Ains

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:16:40