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

技术问询:Google AppScripts下Docs、Presentation能否读写同账号Spreadsheets?

Can Google Apps Script for Docs/Slides interact with same-account Spreadsheets?

Absolutely! You can create, read, and write to Google Spreadsheets linked to your Google account using Google Apps Script—even when your script is bound to other Drive apps like Google Docs, Google Slides, or Google Forms. Below are practical implementations for each operation:

1. Create a New Spreadsheet

Use the SpreadsheetApp.create() method to generate a new spreadsheet directly in your Drive:

function createNewSpreadsheet() {
  // Create spreadsheet with a specified name
  const newSpreadsheet = SpreadsheetApp.create("My Project Tracker");
  
  // Log the URL of the new spreadsheet for easy access
  console.log("New spreadsheet created: " + newSpreadsheet.getUrl());
}

2. Read Data from an Existing Spreadsheet

You can open a spreadsheet either by its unique ID (recommended for reliability) or by its name. Once opened, you can access sheets and retrieve data ranges:

function readSpreadsheetData() {
  // Replace with your target spreadsheet's ID
  const spreadsheetId = "123abcXYZ...";
  
  // Open the spreadsheet by ID
  const spreadsheet = SpreadsheetApp.openById(spreadsheetId);
  
  // Access a specific sheet by name
  const targetSheet = spreadsheet.getSheetByName("Sales Data");
  
  // Get all data in the sheet (from A1 to the last filled cell)
  const allData = targetSheet.getDataRange().getValues();
  
  // Example: Log the second row of data (index 1, since arrays are zero-based)
  console.log("Second row data: " + allData[1]);
}

Note: Using the spreadsheet ID is better than using the name, as multiple files can share the same name in Drive.

3. Write Data to a Spreadsheet

You can write data to specific ranges or append new rows to the bottom of a sheet:

function writeToSpreadsheet() {
  const spreadsheetId = "123abcXYZ...";
  const spreadsheet = SpreadsheetApp.openById(spreadsheetId);
  const targetSheet = spreadsheet.getSheetByName("Sales Data");
  
  // Option 1: Write a 2D array to a specific range
  const newRows = [
    ["Date", "Product", "Revenue"],
    ["2024-05-01", "Laptop", "$999"],
    ["2024-05-02", "Phone", "$699"]
  ];
  // Write starting at cell A1, matching the dimensions of the newRows array
  targetSheet.getRange(1, 1, newRows.length, newRows[0].length).setValues(newRows);
  
  // Option 2: Append a single row to the end of the sheet
  targetSheet.appendRow(["2024-05-03", "Tablet", "$299"]);
}

Key Notes

  • Permissions: The first time you run the script, you’ll need to grant authorization for it to access your Drive files. This is standard for Apps Scripts that interact with Google Workspace services.
  • Cross-App Compatibility: Scripts bound to Docs, Slides, or other Drive apps have the same access permissions as standalone scripts—so as long as the spreadsheet is in your account (or shared with you with edit access), you can interact with it.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:24:31