访问和修改 Google 表格文件。常见操作包括添加新工作表和添加协作者。
方法
详细文档
addDeveloperMetadata(key)
将包含指定键的开发者元数据添加到顶层电子表格。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Adds the key 'NAME' in the developer metadata for the spreadsheet. ss.addDeveloperMetadata('NAME') // Gets the first developer metadata object and logs its key. const developerMetaData = ss.getDeveloperMetadata()[0] console.log(developerMetaData.getKey())
参数
名称 | 类型 | 说明 |
---|---|---|
key | String | 新开发者元数据的键。 |
返程
Spreadsheet
- 此电子表格,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addDeveloperMetadata(key, visibility)
将包含指定键和可见性的开发者元数据添加到电子表格。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Adds the key 'NAME' in the developer metadata for the spreadsheet and sets the visibility // to the developer project that created the metadata. ss.addDeveloperMetadata('NAME', SpreadsheetApp.DeveloperMetadataVisibility.PROJECT); // Gets the first developer metadata object and logs its key and visibility setting. const developerMetaData = ss.getDeveloperMetadata()[0]; console.log(developerMetaData.getKey()); console.log(`Key: ${developerMetaData.getKey()}, . Visibility: ${developerMetaData.getVisibility()}`);
参数
名称 | 类型 | 说明 |
---|---|---|
key | String | 新开发者元数据的键。 |
visibility | DeveloperMetadataVisibility | 新开发者元数据的公开范围。 |
返程
Spreadsheet
- 此电子表格,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addDeveloperMetadata(key, value)
将包含指定键和值的开发者元数据添加到电子表格。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Adds the key 'NAME' and sets the value to 'GOOGLE' in the developer metadata for the // spreadsheet. ss.addDeveloperMetadata('NAME', 'GOOGLE'); // Gets the first developer metadata object and logs its key and value. const developerMetaData = ss.getDeveloperMetadata()[0]; console.log(developerMetaData.getKey()); console.log(`Key: ${developerMetaData.getKey()}, Value: ${developerMetaData.getValue()}`);
参数
名称 | 类型 | 说明 |
---|---|---|
key | String | 新开发者元数据的键。 |
value | String | 新开发者元数据的值。 |
返程
Spreadsheet
- 此电子表格,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addDeveloperMetadata(key, value, visibility)
将包含指定键、值和可见性的开发者元数据添加到电子表格。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Adds the key 'NAME', sets the value to 'GOOGLE', and sets the visibility // to any developer project with document access. ss.addDeveloperMetadata('NAME', 'GOOGLE', SpreadsheetApp.DeveloperMetadataVisibility.DOCUMENT); // Gets the first developer metadata object and logs its key, value, and visibility setting. const developerMetaData = ss.getDeveloperMetadata()[0]; console.log(`Key: ${developerMetaData.getKey()}, Value: ${developerMetaData.getValue()}, Visibility: ${developerMetaData.getVisibility()}`);
参数
名称 | 类型 | 说明 |
---|---|---|
key | String | 新开发者元数据的键。 |
value | String | 新开发者元数据的值。 |
visibility | DeveloperMetadataVisibility | 新开发者元数据的公开范围。 |
返程
Spreadsheet
- 此电子表格,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addEditor(emailAddress)
将指定用户添加到 Spreadsheet
的编辑者列表中。如果用户已位于观看者列表中,则此方法会将用户从观看者列表中提升出来。
参数
名称 | 类型 | 说明 |
---|---|---|
emailAddress | String | 要添加的用户的电子邮件地址。 |
返程
Spreadsheet
- 此 Spreadsheet
,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addEditor(user)
将指定用户添加到 Spreadsheet
的编辑者列表中。如果用户已位于观看者列表中,则此方法会将用户从观看者列表中提升出来。
参数
名称 | 类型 | 说明 |
---|---|---|
user | User | 表示要添加的用户。 |
返程
Spreadsheet
- 此 Spreadsheet
,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addEditors(emailAddresses)
将给定的用户数组添加到 Spreadsheet
的编辑者列表。如果有任何用户已位于查看者列表中,则此方法会将他们从查看者列表中提升出来。
参数
名称 | 类型 | 说明 |
---|---|---|
emailAddresses | String[] | 要添加的用户的电子邮件地址数组。 |
返程
Spreadsheet
- 此 Spreadsheet
,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addMenu(name, subMenus)
在电子表格界面中创建新菜单。
每个菜单项都运行一个用户定义的函数。通常情况下,您需要通过 onOpen()
函数调用该函数,以便在电子表格加载时自动创建菜单。
// The onOpen function is executed automatically every time a Spreadsheet is loaded function onOpen() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var menuEntries = []; // When the user clicks on "addMenuExample" then "Menu Entry 1", the function function1 is // executed. menuEntries.push({name: "Menu Entry 1", functionName: "function1"}); menuEntries.push(null); // line separator menuEntries.push({name: "Menu Entry 2", functionName: "function2"}); ss.addMenu("addMenuExample", menuEntries); }
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 要创建的菜单的名称。 |
subMenus | Object[] | 包含 name 和 functionName
参数的 JavaScript 映射数组。您可以使用所含库中的函数,例如
Library.libFunction1 。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addViewer(emailAddress)
将指定用户添加到 Spreadsheet
的查看者列表中。如果用户已在编辑者列表中,则此方法不会产生任何影响。
参数
名称 | 类型 | 说明 |
---|---|---|
emailAddress | String | 要添加的用户的电子邮件地址。 |
返程
Spreadsheet
- 此 Spreadsheet
,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addViewer(user)
将指定用户添加到 Spreadsheet
的查看者列表中。如果用户已在编辑者列表中,则此方法不会产生任何影响。
参数
名称 | 类型 | 说明 |
---|---|---|
user | User | 表示要添加的用户。 |
返程
Spreadsheet
- 此 Spreadsheet
,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addViewers(emailAddresses)
将指定用户列表添加到 Spreadsheet
的查看者列表。如果有任何用户已在编辑者列表,此方法对他们没有影响。
参数
名称 | 类型 | 说明 |
---|---|---|
emailAddresses | String[] | 要添加的用户的电子邮件地址数组。 |
返程
Spreadsheet
- 此 Spreadsheet
,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
appendRow(rowContents)
向工作表中当前数据区域的底部附加一行。如果单元格的内容以 =
开头,则会被解释为公式。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Appends a new row with 3 columns to the bottom of the current // data region in the sheet containing the values in the array. sheet.appendRow(["a man", "a plan", "panama"]);
参数
名称 | 类型 | 说明 |
---|---|---|
rowContents | Object[] | 要在工作表中的最后一行之后插入的一组值。 |
返程
Sheet
- 工作表,可用于方法链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
autoResizeColumn(columnPosition)
设置指定列的宽度以适应其内容。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; sheet.getRange('a1').setValue('Whenever it is a damp, drizzly November in my soul...'); // Sets the first column to a width which fits the text sheet.autoResizeColumn(1);
参数
名称 | 类型 | 说明 |
---|---|---|
columnPosition | Integer | 要调整大小的指定列的位置。 |
返程
Sheet
- 工作表,可用于方法链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
copy(name)
复制电子表格并返回新电子表格。
// This code makes a copy of the current spreadsheet and names it appropriately var ss = SpreadsheetApp.getActiveSpreadsheet(); ss.copy("Copy of " + ss.getName());
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 副本的名称。 |
返程
Spreadsheet
- 此电子表格,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
createDeveloperMetadataFinder()
返回 DeveloperMetadataFinder
,用于查找此电子表格范围内的开发者元数据。默认情况下,此操作会考虑与电子表格、工作表、行和列关联的所有元数据。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Adds developer metadata to the spreadsheet. ss.addDeveloperMetadata('NAME', 'CHARLIE'); ss.addDeveloperMetadata('COMPANY', 'EXAMPLE ORGANIZATION'); ss.addDeveloperMetadata('TECHNOLOGY', 'JAVASCRIPT'); // Creates a developer metadata finder. const developerMetadataFinder = ss.createDeveloperMetadataFinder(); // Finds the developer metadata objects with 'COMPANY' as the key. const googleMetadataFromSpreadsheet = developerMetadataFinder.withKey('COMPANY').find(); // Gets the first result of developer metadata that has the key 'COMPANY' and logs its value. console.log(googleMetadataFromSpreadsheet[0].getValue());
返程
DeveloperMetadataFinder
- 开发者元数据查找工具,可用于搜索此电子表格范围内的元数据。
createTextFinder(findText)
为电子表格创建文本查找器,可用于查找和替换电子表格内的文本。搜索从电子表格的第一个工作表开始。
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // Creates a text finder. var textFinder = spreadsheet.createTextFinder('dog'); // Returns the first occurrence of 'dog' in the spreadsheet. var firstOccurrence = textFinder.findNext(); // Replaces the last found occurrence of 'dog' with 'cat' and returns the number // of occurrences replaced. var numOccurrencesReplaced = findOccurrence.replaceWith('cat');
参数
名称 | 类型 | 说明 |
---|---|---|
findText | String | 要搜索的文本。 |
返程
TextFinder
- 电子表格的 TextFinder
。
deleteActiveSheet()
删除当前活动工作表。
// The code below deletes the currently active sheet and stores the new active sheet in a // variable var newSheet = SpreadsheetApp.getActiveSpreadsheet().deleteActiveSheet();
返程
Sheet
- 新的活动工作表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
deleteColumn(columnPosition)
删除位于指定列位置的列。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Columns start at "1" - this deletes the first column sheet.deleteColumn(1);
参数
名称 | 类型 | 说明 |
---|---|---|
columnPosition | Integer | 列的位置,第一列从 1 开始。 |
返程
Sheet
- 工作表,可用于方法链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
deleteColumns(columnPosition, howMany)
删除从指定列位置开始的若干列。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Columns start at "1" - this deletes the first two columns sheet.deleteColumns(1, 2);
参数
名称 | 类型 | 说明 |
---|---|---|
columnPosition | Integer | 要删除的第一列的位置。 |
howMany | Integer | 要删除的列数。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
deleteRow(rowPosition)
删除位于指定行位置的行。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Rows start at "1" - this deletes the first row sheet.deleteRow(1);
参数
名称 | 类型 | 说明 |
---|---|---|
rowPosition | Integer | 行的位置,从第一行的 1 开始。 |
返程
Sheet
- 工作表,可用于方法链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
deleteRows(rowPosition, howMany)
删除从指定行位置开始的若干行。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Rows start at "1" - this deletes the first two rows sheet.deleteRows(1, 2);
参数
名称 | 类型 | 说明 |
---|---|---|
rowPosition | Integer | 要删除的第一行的位置。 |
howMany | Integer | 要删除的行数。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
deleteSheet(sheet)
删除指定的工作表。
// The code below deletes the specified sheet. var ss = SpreadsheetApp.getActive(); var sheet = ss.getSheetByName('My Sheet'); ss.deleteSheet(sheet);
参数
名称 | 类型 | 说明 |
---|---|---|
sheet | Sheet | 要删除的工作表。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
duplicateActiveSheet()
getActiveCell()
返回此工作表中的活动单元格。
注意:最好使用 getCurrentCell()
,它会返回当前突出显示的单元格。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Returns the active cell var cell = sheet.getActiveCell();
返程
Range
- 当前的活动单元格
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getActiveRange()
返回有效工作表中的所选范围,如果没有有效范围,则返回 null
。如果选择了多个范围,则此方法仅返回最后选择的范围。
“活动范围”一词是指用户在活动工作表中选择的范围,但在自定义函数中,它是指正在重新计算的单元格。
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var activeRange = sheet.getActiveRange();
返程
Range
- 有效范围
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
另请参阅
getActiveRangeList()
返回有效工作表中的有效范围列表;如果没有有效范围,则返回 null
。
如果选择的是单个范围,则行为就像 getActiveRange()
调用。
var sheet = SpreadsheetApp.getActiveSheet(); // Returns the list of active ranges. var activeRangeList = sheet.getActiveRangeList();
返程
RangeList
- 有效范围的列表
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
另请参阅
getActiveSheet()
获取电子表格中的有效工作表。
电子表格中的有效工作表是指在电子表格界面中显示的工作表。
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
返程
Sheet
- 电子表格中的活动工作表。
getAs(contentType)
返回此对象中的数据作为转换为指定内容类型的 blob。此方法会为文件名添加适当的扩展名,例如“myfile.pdf”。不过,系统会假定文件名中最后一个句点后面的部分(如果有)是应替换的现有扩展名。因此,“ShoppingList.12.25.2014”会变成“ShoppingList.12.25.pdf”。
如需查看转化次数的每日配额,请参阅 Google 服务的配额。新创建的 Google Workspace 网域可能会暂时受更严格的配额限制。
参数
名称 | 类型 | 说明 |
---|---|---|
contentType | String | 要转换为的 MIME 类型。对于大多数 blob,'application/pdf' 是唯一有效的选项。对于 BMP、GIF、JPEG 或 PNG 格式的图片,'image/bmp' 、'image/gif' 、'image/jpeg' 或 'image/png' 中的任意一个也有效。 |
返程
Blob
- 作为 blob 的数据。
getBandings()
返回此电子表格中的所有条带。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets an array of the bandings in the spreadsheet. const bandings = ss.getBandings(); // Logs the range of the first banding in the spreadsheet to the console. console.log(bandings[0].getRange().getA1Notation());
返程
Banding[]
- 此电子表格中的条带。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getBlob()
getColumnWidth(columnPosition)
获取指定列的宽度(以像素为单位)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Columns start at 1 Logger.log(sheet.getColumnWidth(1));
参数
名称 | 类型 | 说明 |
---|---|---|
columnPosition | Integer | 要检查的列的位置。 |
返程
Integer
- 列宽(以像素为单位)
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getCurrentCell()
返回活动工作表中的当前单元格;如果没有当前单元格,则返回 null
。当前单元格是 Google 表格界面中具有焦点的单元格,并用深色边框突出显示。当前单元格数量不超过 1 个。当用户选择一个或多个单元格范围时,所选范围内的某个单元格即为当前单元格。
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // Returns the current highlighted cell in the one of the active ranges. var currentCell = sheet.getCurrentCell();
返程
Range
- 当前单元格
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataRange()
返回与数据存在的维度对应的 Range
。
这在功能上等同于创建一个由 A1 和(Sheet.getLastColumn()、Sheet.getLastRow())限定的范围。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This represents ALL the data var range = sheet.getDataRange(); var values = range.getValues(); // This logs the spreadsheet in CSV format with a trailing comma for (var i = 0; i < values.length; i++) { var row = ""; for (var j = 0; j < values[i].length; j++) { if (values[i][j]) { row = row + values[i][j]; } row = row + ","; } Logger.log(row); }
返程
Range
- 包含电子表格中所有数据的范围
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataSourceFormulas()
获取所有数据源公式。
// Opens the spreadsheet by its ID. If you created your script from within a Google Sheets // file, use SpreadsheetApp.getActiveSpreadsheet(). // TODO(developer): Replace the ID with your own. const ss = SpreadsheetApp.openById('abc123456'); // Gets Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets an array of the data source formulas on Sheet1. // To get an array of data source formulas for the entire spreadsheet, // replace 'sheet' with 'ss'. const dataSourceFormulas = sheet.getDataSourceFormulas(); // Logs the first data source formula in the array. console.log(dataSourceFormulas[0].getFormula());
返程
DataSourceFormula[]
- 数据源公式列表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataSourcePivotTables()
获取所有数据源数据透视表。
// Opens the spreadsheet file by its ID. If you created your script from a Google Sheets file, // use SpreadsheetApp.getActiveSpreadsheet(). // TODO(developer): Replace the ID with your own. const ss = SpreadsheetApp.openById('abc123456'); // Gets Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets an array of the data source pivot tables on Sheet1. // To get an array of data source pivot tables for the entire // spreadsheet, replace 'sheet' with 'ss'. const dataSourcePivotTables = sheet.getDataSourcePivotTables(); // Logs the last time that the first pivot table in the array was refreshed. console.log(dataSourcePivotTables[0].getStatus().getLastRefreshedTime());
返程
DataSourcePivotTable[]
- 数据源数据透视表的列表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataSourceRefreshSchedules()
获取此电子表格的刷新时间表。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Activates BigQuery operations for the connected spreadsheet. SpreadsheetApp.enableBigQueryExecution(); // Gets the frequency type of the first referesh schedule in the array. const frequencyType = ss.getDataSourceRefreshSchedules()[0] .getFrequency() .getFrequencyType() .toString(); // Logs the frequency type to the console. console.log(frequencyType);
返程
DataSourceRefreshSchedule[]
- 此电子表格的刷新时间表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataSourceSheets()
返回电子表格中的所有数据源表格。
// Turns data execution on for BigQuery data sources. SpreadsheetApp.enableBigQueryExecution(); // Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets the first data source sheet in the spreadsheet. const dataSource = ss.getDataSourceSheets()[0]; // Gets the name of the data source sheet. console.log(dataSource.asSheet().getName());
返程
DataSourceSheet[]
- 包含所有数据源工作表的数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataSourceTables()
获取所有数据源表。
// Opens the spreadsheet file by its ID. If you created your script from a Google Sheets file, // use SpreadsheetApp.getActiveSpreadsheet(). // TODO(developer): Replace the ID with your own. const ss = SpreadsheetApp.openById('abc123456'); // Gets Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets an array of data source tables on Sheet1. // To get an array of data source tables for the entire spreadsheet, // replace 'sheet' with 'ss'. const dataSourceTables = sheet.getDataSourceTables(); // Logs the last completed data execution time on the first data source table. console.log(dataSourceTables[0].getStatus().getLastExecutionTime());
返程
DataSourceTable[]
- 数据源表的列表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataSources()
返回电子表格中的所有数据源。
// Turns data execution on for BigQuery data sources. SpreadsheetApp.enableBigQueryExecution(); // Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets the data sources on the spreadsheet. const dataSources = ss.getDataSources(); // Logs the name of the first column on the first data source. console.log(dataSources[0].getColumns()[0].getName());
返程
DataSource[]
- 包含所有数据源的数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDeveloperMetadata()
获取与顶级电子表格关联的开发者元数据。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Adds 'Google' as a key to the spreadsheet metadata. ss.addDeveloperMetadata('Google'); // Gets the spreadsheet's metadata. const ssMetadata = ss.getDeveloperMetadata(); // Gets the first set of the spreadsheet's metadata and logs the key to the console. console.log(ssMetadata[0].getKey());
返程
DeveloperMetadata[]
- 与此范围关联的开发者元数据。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getEditors()
获取此 Spreadsheet
的编辑者列表。
返程
User[]
- 具有修改权限的用户数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFormUrl()
返回将其响应发送到此电子表格的表单的网址,如果此电子表格没有关联的表单,则返回 null
。如果有多个表单向此电子表格发送回复,返回的表单网址将是不确定的。作为替代方案,您可以通过 Sheet.getFormUrl()
方法检索每个工作表表单的网址关联。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets the form URL from the spreadsheet. const formUrl = ss.getFormUrl(); // Logs the form URL to the console. console.log(formUrl);
返程
String
- 将回复放入此电子表格的表单的网址,如果此电子表格没有关联表单,则为 null
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFrozenColumns()
返回冻结列的数量。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; Logger.log("Number of frozen columns: %s", sheet.getFrozenColumns());
返程
Integer
- 冻结列的数量
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFrozenRows()
返回冻结行的数量。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; Logger.log("Number of frozen rows: %s", sheet.getFrozenRows());
返程
Integer
- 冻结行的数量
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getId()
获取此电子表格的唯一标识符。您可以从电子表格 ID 中提取电子表格 ID。 例如,以下网址中的电子表格 ID 为“abc1234567”。
// The code below logs the ID for the active spreadsheet. Logger.log(SpreadsheetApp.getActiveSpreadsheet().getId());
返程
String
- 电子表格的唯一 ID(或键)。
getImages()
返回工作表上的所有网格内图片。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets the over-the-grid images from Sheet1. // To get the over-the-grid images from the entire spreadsheet, use ss.getImages() instead. const images = sheet.getImages(); // For each image, logs the anchor cell in A1 notation. for (const image of images) { console.log(image.getAnchorCell().getA1Notation()); }
返程
OverGridImage[]
- 一组网格上图片。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getIterativeCalculationConvergenceThreshold()
返回迭代计算中使用的阈值。当连续计算的结果差异小于此值时,迭代计算停止。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Sets the iterative calculation convergence threshold for the spreadsheet. ss.setIterativeCalculationConvergenceThreshold(2); // Logs the threshold to the console. console.log(ss.getIterativeCalculationConvergenceThreshold());
返程
Number
- 收敛阈值。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getLastColumn()
返回包含内容的最后一列的位置。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This logs the value in the very last cell of this sheet var lastRow = sheet.getLastRow(); var lastColumn = sheet.getLastColumn(); var lastCell = sheet.getRange(lastRow, lastColumn); Logger.log(lastCell.getValue());
返程
Integer
- 工作表中包含内容的最后一列
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getLastRow()
返回包含内容的最后一行的位置。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This logs the value in the very last cell of this sheet var lastRow = sheet.getLastRow(); var lastColumn = sheet.getLastColumn(); var lastCell = sheet.getRange(lastRow, lastColumn); Logger.log(lastCell.getValue());
返程
Integer
- 工作表中包含内容的最后一行
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getMaxIterativeCalculationCycles()
返回迭代计算期间使用的最大迭代次数。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Sets the max iterative calculation cycles for the spreadsheet. ss.setMaxIterativeCalculationCycles(10); // Logs the max iterative calculation cycles to the console. console.log(ss.getMaxIterativeCalculationCycles());
返程
Integer
- 计算迭代次数上限。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getName()
获取文档的名称。
var ss = SpreadsheetApp.getActiveSpreadsheet(); Logger.log(ss.getName());
返程
String
- 电子表格的名称。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getNamedRanges()
获取此电子表格中的所有命名范围。
// The code below logs the name of the first named range. var namedRanges = SpreadsheetApp.getActiveSpreadsheet().getNamedRanges(); for (var i = 0; i < namedRanges.length; i++) { Logger.log(namedRanges[i].getName()); }
返程
NamedRange[]
- 电子表格中所有命名范围的数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getNumSheets()
返回此电子表格中的工作表数量。
// The code below logs the number of sheets in the active spreadsheet. Logger.log(SpreadsheetApp.getActiveSpreadsheet().getNumSheets());
返程
Integer
- 电子表格中的工作表数量。
getOwner()
返回文档的所有者;对于共享云端硬盘中的文档,返回 null
。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var owner = ss.getOwner(); Logger.log(owner.getEmail());
返程
User
- 文档的所有者,如果文档在共享云端硬盘中,则为 null
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getPredefinedSpreadsheetThemes()
返回预定义主题的列表。
// The code below returns the list of predefined themes. var predefinedThemesList = SpreadsheetApp.getActiveSpreadsheet() .getPredefinedSpreadsheetThemes();
返程
SpreadsheetTheme[]
- 预定义主题列表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getProtections(type)
获取代表电子表格中所有受保护范围或工作表的对象数组。
// Remove all range protections in the spreadsheet that the user has permission to edit. var ss = SpreadsheetApp.getActive(); var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (var i = 0; i < protections.length; i++) { var protection = protections[i]; if (protection.canEdit()) { protection.remove(); } }
// Remove all sheet protections in the spreadsheet that the user has permission to edit. var ss = SpreadsheetApp.getActive(); var protections = ss.getProtections(SpreadsheetApp.ProtectionType.SHEET); for (var i = 0; i < protections.length; i++) { var protection = protections[i]; if (protection.canEdit()) { protection.remove(); } }
参数
名称 | 类型 | 说明 |
---|---|---|
type | ProtectionType | 受保护的区域的类型,即 SpreadsheetApp.ProtectionType.RANGE 或 SpreadsheetApp.ProtectionType.SHEET 。 |
返程
Protection[]
- 表示电子表格中所有受保护范围或工作表的对象数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getRange(a1Notation)
返回以 A1 表示法或 R1C1 表示法指定的范围。
// Get a range A1:D4 on sheet titled "Invoices" var ss = SpreadsheetApp.getActiveSpreadsheet(); var range = ss.getRange("Invoices!A1:D4"); // Get cell A1 on the first sheet var sheet = ss.getSheets()[0]; var cell = sheet.getRange("A1");
参数
名称 | 类型 | 说明 |
---|---|---|
a1Notation | String | 要返回的范围,采用 A1 表示法或 R1C1 表示法指定。 |
返程
Range
- 指定位置处的范围
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getRangeByName(name)
返回命名的范围,如果未找到具有指定名称的范围,则返回 null
。如果电子表格的多个工作表使用相同的范围名称,请指定工作表名称,不要添加额外的引号。例如,可以指定 getRangeByName('TaxRates')
或 getRangeByName('Sheet Name!TaxRates')
,但不能指定 getRangeByName('"Sheet
Name"!TaxRates')
。
// Log the number of columns for the range named 'TaxRates' in the active spreadsheet. var range = SpreadsheetApp.getActiveSpreadsheet().getRangeByName('TaxRates'); if (range != null) { Logger.log(range.getNumColumns()); }
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 要获取的范围的名称。 |
返程
Range
- 具有指定名称的单元格范围。
getRangeList(a1Notations)
返回 RangeList
集合,该集合表示由 A1 表示法或 R1C1 表示法的非空列表指定的同一工作表中的范围。
// Get a list of ranges A1:D4, F1:H4. var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var rangeList = sheet.getRangeList(['A1:D4', 'F1:H4']);
参数
名称 | 类型 | 说明 |
---|---|---|
a1Notations | String[] | 要返回的范围列表,采用 A1 表示法或 R1C1 表示法指定。 |
返程
RangeList
- 指定位置处的范围列表
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getRecalculationInterval()
返回此电子表格的计算间隔。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Logs the calculation interval for the spreadsheet to the console. console.log(ss.getRecalculationInterval().toString());
返程
RecalculationInterval
- 此电子表格的计算间隔。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getRowHeight(rowPosition)
获取指定行的高度(以像素为单位)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Rows start at 1 Logger.log(sheet.getRowHeight(1));
参数
名称 | 类型 | 说明 |
---|---|---|
rowPosition | Integer | 要检查的行的位置。 |
返程
Integer
- 行高(以像素为单位)
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getSelection()
getSheetByName(name)
返回具有指定名称的工作表。
如果多个工作表具有相同的名称,则返回最左边的工作表。如果不存在具有指定名称的工作表,则返回 null
。
// The code below logs the index of a sheet named "Expenses" var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Expenses"); if (sheet != null) { Logger.log(sheet.getIndex()); }
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 要获取的工作表的名称。 |
返程
Sheet
- 具有指定名称的工作表。
getSheetId()
返回此对象表示的工作表的 ID。
这是相应工作表的专属 ID。此 ID 是在创建工作表时分配的单调递增整数,与工作表位置无关。这在与 Range.copyFormatToRange(gridId, column, columnEnd, row, rowEnd)
等方法结合使用时很有用,这些方法采用 gridId
参数而不是 Sheet
实例。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; Logger.log(sheet.getSheetId());
返程
Integer
- 电子表格所独有的工作表 ID
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getSheetName()
返回工作表名称。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; Logger.log(sheet.getSheetName());
返程
String
- 工作表的名称
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getSheetValues(startRow, startColumn, numRows, numColumns)
返回此范围值(从指定坐标开始)的矩形网格。将行或列指定为 -1 的值相当于获取工作表中数据的最后一行或最后一列。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // The two samples below produce the same output var values = sheet.getSheetValues(1, 1, 3, 3); Logger.log(values); var range = sheet.getRange(1, 1, 3, 3); values = range.getValues(); Logger.log(values);
参数
名称 | 类型 | 说明 |
---|---|---|
startRow | Integer | 起始行的位置。 |
startColumn | Integer | 起始列的位置。 |
numRows | Integer | 要返回值的行数。 |
numColumns | Integer | 要返回其值的列数。 |
返程
Object[][]
- 二维值数组
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getSheets()
获取此电子表格中的所有工作表。
// The code below logs the name of the second sheet var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets(); if (sheets.length > 1) { Logger.log(sheets[1].getName()); }
返程
Sheet[]
- 电子表格中所有工作表的数组。
getSpreadsheetLocale()
获取电子表格语言区域。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets the spreadsheet locale. const ssLocale = ss.getSpreadsheetLocale(); // Logs the locale to the console. console.log(ssLocale);
返程
String
- 电子表格语言区域。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getSpreadsheetTheme()
返回电子表格的当前主题背景;如果未应用任何主题背景,则返回 null
。
// The code below returns the current theme of the spreadsheet. var currentTheme = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTheme();
返程
SpreadsheetTheme
- 当前应用的主题。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getSpreadsheetTimeZone()
获取电子表格的时区。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Sets the time zone of the spreadsheet. ss.setSpreadsheetTimeZone('America/New_York'); // Gets the time zone of the spreadsheet. const ssTimeZone = ss.getSpreadsheetTimeZone(); // Logs the time zone to the console. console.log(ssTimeZone);
返程
String
- 以“long”格式指定的时区(例如,Joda.org 列出的“America/New_York”)。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getUrl()
返回指定电子表格的网址。
var ss = SpreadsheetApp.getActiveSpreadsheet(); Logger.log(ss.getUrl());
返程
String
- 指定电子表格的网址。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getViewers()
获取此 Spreadsheet
的查看者和评论者列表。
返程
User[]
- 具有查看或评论权限的用户数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
hideColumn(column)
隐藏指定范围内的一列或多列。
let ss = SpreadsheetApp.getActiveSpreadsheet(); let sheet = ss.getSheets()[0]; // This hides the first column let range = sheet.getRange("A1"); sheet.hideColumn(range); // This hides the first 3 columns let range = sheet.getRange("A:C"); sheet.hideColumn(range);
参数
名称 | 类型 | 说明 |
---|---|---|
column | Range | 要隐藏的列范围。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
hideRow(row)
隐藏指定范围内的行。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This hides the first row var range = sheet.getRange("A1"); sheet.hideRow(range);
参数
名称 | 类型 | 说明 |
---|---|---|
row | Range | 要隐藏的行范围。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertColumnAfter(afterPosition)
在指定列位置后插入一列。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This inserts a column after the first column position sheet.insertColumnAfter(1);
参数
名称 | 类型 | 说明 |
---|---|---|
afterPosition | Integer | 应在其后添加新列的列。 |
返程
Sheet
- 工作表,可用于方法链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertColumnBefore(beforePosition)
在指定列位置之前插入一列。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This inserts a column in the first column position sheet.insertColumnBefore(1);
参数
名称 | 类型 | 说明 |
---|---|---|
beforePosition | Integer | 应在其前添加新列的列。 |
返程
Sheet
- 工作表,可用于方法链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertColumnsAfter(afterPosition, howMany)
在给定列位置后插入一定列数。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This inserts a column in the second column position sheet.insertColumnsAfter(1);
参数
名称 | 类型 | 说明 |
---|---|---|
afterPosition | Integer | 应在其后添加新列的列。 |
howMany | Integer | 要插入的列数。 |
返程
Sheet
- 工作表,可用于方法链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertColumnsBefore(beforePosition, howMany)
在指定列位置之前插入多个列。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This inserts five columns before the first column sheet.insertColumnsBefore(1, 5);
参数
名称 | 类型 | 说明 |
---|---|---|
beforePosition | Integer | 应在其前添加新列的列。 |
howMany | Integer | 要插入的列数。 |
返程
Sheet
- 工作表,可用于方法链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertDataSourceSheet(spec)
在电子表格中插入新的 DataSourceSheet
并开始数据执行。附带好处:这也会将新工作表设为活动工作表。
如果数据源类型未启用,则会抛出异常。使用 SpreadsheetApp#enable...Execution()
方法针对特定数据源类型启用数据执行。
// Activates BigQuery operations. SpreadsheetApp.enableBigQueryExecution(); // Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Builds a data source specification. // TODO (developer): Update the project ID to your own Google Cloud project ID. const dataSourceSpec = SpreadsheetApp.newDataSourceSpec() .asBigQuery() .setProjectId('project-id-1') .setTableProjectId('bigquery-public-data') .setDatasetId('ncaa_basketball') .setTableId('mbb_historical_teams_games') .build(); // Adds the data source and its data to the spreadsheet. ss.insertDataSourceSheet(dataSourceSpec);
参数
名称 | 类型 | 说明 |
---|---|---|
spec | DataSourceSpec | 要插入的数据源规范。 |
返程
DataSourceSheet
- 新的数据源工作表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertImage(blobSource, column, row)
在文档的指定行和列处插入 Spreadsheet
作为图片。系统会从 blob 内容中检索图像大小。支持的 blob 大小上限为 2MB。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var blob = Utilities.newBlob(binaryData, 'image/png', 'MyImageName'); sheet.insertImage(blob, 1, 1);
参数
名称 | 类型 | 说明 |
---|---|---|
blobSource | BlobSource | 包含图片内容、MIME 类型和(可选)名称的 blob。 |
column | Integer | 列位置。 |
row | Integer | 行位置。 |
返程
OverGridImage
- 插入的图片。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertImage(blobSource, column, row, offsetX, offsetY)
在文档的指定行和列处以图片形式插入 Spreadsheet
,并具有像素偏移量。系统会从 blob 内容中检索图像大小。支持的 blob 大小上限为 2MB。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var blob = Utilities.newBlob(binaryData, 'image/png', 'MyImageName'); sheet.insertImage(blob, 1, 1, 10, 10);
参数
名称 | 类型 | 说明 |
---|---|---|
blobSource | BlobSource | 包含图片内容、MIME 类型和(可选)名称的 blob。 |
column | Integer | 列位置。 |
row | Integer | 行位置。 |
offsetX | Integer | 相对于单元格角的水平偏移量(以像素为单位)。 |
offsetY | Integer | 相对于单元格角的垂直偏移量(以像素为单位)。 |
返程
OverGridImage
- 插入的图片。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertImage(url, column, row)
在文档中给定的行和列处插入图片。
提供的网址必须可以公开访问。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; sheet.insertImage("https://www.google.com/images/srpr/logo3w.png", 1, 1);
参数
名称 | 类型 | 说明 |
---|---|---|
url | String | 图片的网址。 |
column | Integer | 网格列位置。 |
row | Integer | 网格行位置。 |
返程
OverGridImage
- 插入的图片。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertImage(url, column, row, offsetX, offsetY)
在文档中的指定行和列处插入图片,并偏移像素值。
提供的网址必须可以公开访问。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; sheet.insertImage("https://www.google.com/images/srpr/logo3w.png", 1, 1, 10, 10);
参数
名称 | 类型 | 说明 |
---|---|---|
url | String | 图片的网址。 |
column | Integer | 列位置。 |
row | Integer | 行位置。 |
offsetX | Integer | 相对于单元格角的水平偏移量(以像素为单位)。 |
offsetY | Integer | 相对于单元格角的垂直偏移量(以像素为单位)。 |
返程
OverGridImage
- 已插入的图片。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertRowAfter(afterPosition)
在给定行位置之后插入一行。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This inserts a row after the first row position sheet.insertRowAfter(1);
参数
名称 | 类型 | 说明 |
---|---|---|
afterPosition | Integer | 应在其后添加新行的行。 |
返程
Sheet
- 工作表,可用于方法链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertRowBefore(beforePosition)
在指定行位置之前插入一行。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This inserts a row before the first row position sheet.insertRowBefore(1);
参数
名称 | 类型 | 说明 |
---|---|---|
beforePosition | Integer | 应在其前添加新行的行。 |
返程
Sheet
- 工作表,可用于方法链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertRowsAfter(afterPosition, howMany)
在给定行位置之后插入一定数量的行。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This inserts five rows after the first row sheet.insertRowsAfter(1, 5);
参数
名称 | 类型 | 说明 |
---|---|---|
afterPosition | Integer | 应在其后添加新行的行。 |
howMany | Integer | 要插入的行数。 |
返程
Sheet
- 工作表,可用于方法链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertRowsBefore(beforePosition, howMany)
在给定行位置之前插入若干行。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This inserts five rows before the first row sheet.insertRowsBefore(1, 5);
参数
名称 | 类型 | 说明 |
---|---|---|
beforePosition | Integer | 应在其前添加新行的行。 |
howMany | Integer | 要插入的行数。 |
返程
Sheet
- 工作表,可用于方法链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertSheet()
insertSheet(sheetIndex)
在电子表格中的指定索引处插入新工作表。新工作表会变为活动工作表。
var ss = SpreadsheetApp.getActiveSpreadsheet(); ss.insertSheet(1);
参数
名称 | 类型 | 说明 |
---|---|---|
sheetIndex | Integer | 新创建的工作表的索引。如要将某个工作表作为电子表格中的第一个工作表插入,请将其设置为 0。 |
返程
Sheet
- 新工作表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertSheet(sheetIndex, options)
将新工作表插入电子表格中的指定索引处,并使用可选的高级参数。新工作表就会成为活动工作表。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var templateSheet = ss.getSheetByName('Sales'); ss.insertSheet(1, {template: templateSheet});
参数
名称 | 类型 | 说明 |
---|---|---|
sheetIndex | Integer | 新创建的工作表的索引。如要将某个工作表作为电子表格中的第一个工作表插入,请将其设置为 0。 |
options | Object | 可选的 JavaScript 高级参数。 |
高级参数
名称 | 类型 | 说明 |
---|---|---|
template | Sheet | 模板工作表对象中的所有数据都会复制到新工作表中。工作表模板必须是电子表格对象的其中一个工作表。 |
返程
Sheet
- 新工作表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertSheet(options)
使用默认工作表名称和可选的高级参数,将新的工作表插入电子表格中。新工作表就会成为活动工作表。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var templateSheet = ss.getSheetByName('Sales'); ss.insertSheet({template: templateSheet});
参数
名称 | 类型 | 说明 |
---|---|---|
options | Object | 可选的 JavaScript 高级参数(如下所示)。 |
高级参数
名称 | 类型 | 说明 |
---|---|---|
template | Sheet | 模板工作表对象中的所有数据都会复制到新工作表中。工作表模板必须是此电子表格对象的一个工作表。 |
返程
Sheet
- 新工作表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertSheet(sheetName)
insertSheet(sheetName, sheetIndex)
将具有指定名称的电子表格中的指定索引处的新工作表插入。新工作表会变为有效工作表。
var ss = SpreadsheetApp.getActiveSpreadsheet(); ss.insertSheet('My New Sheet', 1);
参数
名称 | 类型 | 说明 |
---|---|---|
sheetName | String | 新工作表的名称。 |
sheetIndex | Integer | 新创建的工作表的索引。如要将某个工作表作为电子表格中的第一个工作表插入,请将其设置为 0。 |
返程
Sheet
- 新工作表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertSheet(sheetName, sheetIndex, options)
将具有指定索引处的指定名称的新工作表插入电子表格,并使用可选的高级参数。新工作表就会成为活动工作表。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var templateSheet = ss.getSheetByName('Sales'); ss.insertSheet('My New Sheet', 1, {template: templateSheet});
参数
名称 | 类型 | 说明 |
---|---|---|
sheetName | String | 新工作表的名称。 |
sheetIndex | Integer | 新插入的工作表的索引。如要将某个工作表作为电子表格中的第一个工作表插入,请将其设置为 0。 |
options | Object | 可选的 JavaScript 高级参数。 |
高级参数
名称 | 类型 | 说明 |
---|---|---|
template | Sheet | 模板工作表对象中的所有数据都会复制到新工作表中。工作表模板必须是电子表格对象的其中一个工作表。 |
返程
Sheet
- 新工作表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertSheet(sheetName, options)
将具有指定名称的新工作表插入电子表格中,并使用可选的高级参数。新工作表就会成为活动工作表。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var templateSheet = ss.getSheetByName('Sales'); ss.insertSheet('My New Sheet', {template: templateSheet});
参数
名称 | 类型 | 说明 |
---|---|---|
sheetName | String | 新工作表的名称。 |
options | Object | 可选的 JavaScript 高级参数。 |
高级参数
名称 | 类型 | 说明 |
---|---|---|
template | Sheet | 模板工作表对象中的所有数据都会复制到新工作表中。工作表模板必须是电子表格对象的其中一个工作表。 |
返程
Sheet
- 新工作表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertSheetWithDataSourceTable(spec)
在电子表格中插入新工作表,使用给定数据源规范跨整个工作表创建 DataSourceTable
,然后开始执行数据。附带的作用是,将新工作表设为活动工作表。
如果数据源类型未启用,则会抛出异常。使用 SpreadsheetApp#enable...Execution()
方法针对特定数据源类型启用数据执行。
// Activates BigQuery operations. SpreadsheetApp.enableBigQueryExecution(); // Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Adds a sheet and sets cell A1 as the parameter cell. const parameterCell = ss.insertSheet('parameterSheet').getRange('A1'); // Sets the value of the parameter cell to 'Duke'. parameterCell.setValue('Duke'); const query = 'select * from `bigquery-public-data`.`ncaa_basketball`.' + '`mbb_historical_tournament_games` WHERE win_school_ncaa = @SCHOOL'; // Adds a data source with a query parameter. // TODO(developer): Update the project ID to your own Google Cloud project ID. const dataSourceSpec = SpreadsheetApp.newDataSourceSpec() .asBigQuery() .setProjectId('project-id-1') .setRawQuery(query) .setParameterFromCell('SCHOOL', 'parameterSheet!A1') .build(); // Adds sheets for the data source and data source table to the spreadsheet. ss.insertSheetWithDataSourceTable(dataSourceSpec);
参数
名称 | 类型 | 说明 |
---|---|---|
spec | DataSourceSpec | 要插入的数据源规范。 |
返程
Sheet
- 新工作表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
isColumnHiddenByUser(columnPosition)
返回用户是否隐藏了指定列。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Columns start at 1 Logger.log(sheet.isColumnHiddenByUser(1));
参数
名称 | 类型 | 说明 |
---|---|---|
columnPosition | Integer | 要检查的列的位置。 |
返程
Boolean
- 如果列隐藏,则为 true
;否则为 false
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
isIterativeCalculationEnabled()
返回此电子表格中是否激活迭代计算的指示值。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Activates iterative calculation on the spreadsheet. ss.setIterativeCalculationEnabled(true); // Logs whether iterative calculation is activated for the spreadsheet. console.log(ss.isIterativeCalculationEnabled());
返程
Boolean
- 如果启用了迭代计算,则为 true
,否则为 false
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
isRowHiddenByFilter(rowPosition)
返回给定行是否被过滤器(而不是过滤视图)隐藏。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Rows start at 1 Logger.log(sheet.isRowHiddenByFilter(1));
参数
名称 | 类型 | 说明 |
---|---|---|
rowPosition | Integer | 要检查的行的位置。 |
返程
Boolean
- 如果行隐藏,则为 true
;否则为 false
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
isRowHiddenByUser(rowPosition)
返回用户是否隐藏了指定行。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Rows start at 1 Logger.log(sheet.isRowHiddenByUser(1));
参数
名称 | 类型 | 说明 |
---|---|---|
rowPosition | Integer | 要检查的行的位置。 |
返程
Boolean
- 如果行隐藏,则为 true
;否则为 false
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
moveActiveSheet(pos)
将活动工作表移至工作表列表中的指定位置。如果位置为负数或大于工作表数,则会抛出异常。
// This example assumes that there are 2 sheets in the current // active spreadsheet: one named "first" in position 1 and another named "second" // in position 2. var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // Gets the "first" sheet and activates it. var sheet = spreadsheet.getSheetByName("first").activate(); // Logs 'Current index of sheet: 1' console.log("Current index of sheet: %s", sheet.getIndex()); spreadsheet.moveActiveSheet(2); // Logs 'New index of sheet: 2' console.log("New index of sheet: %s", sheet.getIndex());
参数
名称 | 类型 | 说明 |
---|---|---|
pos | Integer | 要将活动工作表移动到工作表列表中的 1 索引位置。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
moveChartToObjectSheet(chart)
创建一个新的 SheetType.OBJECT
工作表,并将提供的图表移动到该工作表中。如果图表已在其自己的工作表中,则会返回该图表,而不会创建新工作表。
var sheet = SpreadsheetApp.getActiveSheet(); var chart = sheet.newChart().setPosition(1, 1, 0, 0).build(); sheet.insertChart(chart); var objectSheet = SpreadsheetApp.getActive().moveChartToObjectSheet(chart);
参数
名称 | 类型 | 说明 |
---|---|---|
chart | EmbeddedChart | 要移动的图表。 |
返程
Sheet
- 图表所在的工作表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
refreshAllDataSources()
刷新所有受支持的数据源及其关联的数据源对象,跳过无效的数据源对象。
使用 SpreadsheetApp#enable...Execution()
方法针对特定数据源类型启用数据执行。
// Activates BigQuery operations. SpreadsheetApp.enableBigQueryExecution(); // Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets the first data source sheet on the spreadsheet. const dataSheet = ss.getDataSourceSheets()[0]; // Refreshes all data sources on the spreadsheet. ss.refreshAllDataSources(); // Logs the last refreshed time of the first data source sheet. console.log(`Last refresh time: ${dataSheet.getStatus().getLastRefreshedTime()}`);
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeEditor(emailAddress)
从 Spreadsheet
的编辑者列表中移除指定用户。如果用户属于具有常规访问权限的一类用户(例如,如果 Spreadsheet
与用户的整个网域共享,或者 Spreadsheet
位于用户可以访问的共享云端硬盘中),则此方法不会阻止用户访问 Spreadsheet
。
对于云端硬盘文件,此操作还会将相应用户从查看者列表中移除。
参数
名称 | 类型 | 说明 |
---|---|---|
emailAddress | String | 要移除的用户的电子邮件地址。 |
返程
Spreadsheet
- 此 Spreadsheet
,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeEditor(user)
从 Spreadsheet
的编辑者列表中移除指定用户。如果用户属于具有常规访问权限的一类用户(例如,如果 Spreadsheet
与用户的整个网域共享,或者 Spreadsheet
位于用户可以访问的共享云端硬盘中),则此方法不会阻止用户访问 Spreadsheet
。
对于云端硬盘文件,此操作还会将相应用户从查看者列表中移除。
参数
名称 | 类型 | 说明 |
---|---|---|
user | User | 表示要移除的用户。 |
返程
Spreadsheet
- 此 Spreadsheet
,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeMenu(name)
移除 addMenu(name, subMenus)
添加的菜单。name
参数的值应与对 addMenu(name, subMenus)
的相应调用相同。
// The onOpen function is executed automatically every time a Spreadsheet is loaded function onOpen() { var ss = SpreadsheetApp.getActiveSpreadsheet(); ss.addMenu("badMenu", [{name: "remove bad menu", functionName: "removeBadMenu"}, {name: "foo", functionName: "foo"}]); } function removeBadMenu() { var ss = SpreadsheetApp.getActiveSpreadsheet(); ss.removeMenu("badMenu"); // name must match the name used when added the menu } function foo(){ // Do nothing }
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 要移除的菜单的名称。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeNamedRange(name)
删除具有指定名称的命名范围。如果在电子表格中找不到具有指定名称的范围,则会抛出异常。
// The code below creates a new named range "foo", and then remove it. var ss = SpreadsheetApp.getActiveSpreadsheet(); ss.setNamedRange("foo", ss.getActiveRange()); ss.removeNamedRange("foo");
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 范围名称。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeViewer(emailAddress)
从 Spreadsheet
的查看者和评论者列表中移除指定用户。如果用户是编辑者,而不是查看者或评论者,则此方法无效。如果用户属于具有常规访问权限的一类用户(例如,如果 Spreadsheet
与用户的整个网域共享,或者 Spreadsheet
位于用户可以访问的共享云端硬盘中),则此方法不会阻止用户访问 Spreadsheet
。
对于云端硬盘文件,此操作也会将相应用户从编辑者列表中移除。
参数
名称 | 类型 | 说明 |
---|---|---|
emailAddress | String | 要移除的用户的电子邮件地址。 |
返程
Spreadsheet
- 此用于链接的 Spreadsheet
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeViewer(user)
从 Spreadsheet
的查看者和评论者列表中移除指定用户。如果用户是编辑者,而不是查看者,则此方法无效。如果用户属于具有常规访问权限的一类用户,例如,如果 Spreadsheet
与用户的整个网域共享,或者 Spreadsheet
位于用户可以访问的共享云端硬盘中,则此方法也不会阻止用户访问 Spreadsheet
。
对于云端硬盘文件,此操作也会将相应用户从编辑者列表中移除。
参数
名称 | 类型 | 说明 |
---|---|---|
user | User | 表示要移除的用户。 |
返程
Spreadsheet
- 此用于链接的 Spreadsheet
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
rename(newName)
重命名文档。
var ss = SpreadsheetApp.getActiveSpreadsheet(); ss.rename("This is the new name");
参数
名称 | 类型 | 说明 |
---|---|---|
newName | String | 文档的新名称。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
renameActiveSheet(newName)
将当前活动工作表重命名为指定的新名称。
// The code below renames the active sheet to "Hello world" SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet("Hello world");
参数
名称 | 类型 | 说明 |
---|---|---|
newName | String | 当前活动工作表的新名称。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
resetSpreadsheetTheme()
移除已应用的主题背景,并设置默认主题背景。
// The code below applies default theme on the spreadsheet. SpreadsheetApp.getActiveSpreadsheet().resetSpreadsheetTheme();
返程
SpreadsheetTheme
- 默认主题。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setActiveRange(range)
将指定范围设为活动工作表中的 active range
,并将范围中左上角的单元格设为 current cell
。
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var range = sheet.getRange('A1:D4'); sheet.setActiveRange(range); var selection = sheet.getSelection(); // Current cell: A1 var currentCell = selection.getCurrentCell(); // Active Range: A1:D4 var activeRange = selection.getActiveRange();
参数
名称 | 类型 | 说明 |
---|---|---|
range | Range | 要设为有效范围的范围。 |
返程
Range
- 新的有效范围
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setActiveRangeList(rangeList)
将指定的范围列表设置为活动工作表中的 active ranges
。列表中的最后一个范围被设置为 active range
。
var sheet = SpreadsheetApp.getActiveSheet(); var rangeList = sheet.getRangeList(['D4', 'B2:C4']); sheet.setActiveRangeList(rangeList); var selection = sheet.getSelection(); // Current cell: B2 var currentCell = selection.getCurrentCell(); // Active range: B2:C4 var activeRange = selection.getActiveRange(); // Active range list: [D4, B2:C4] var activeRangeList = selection.getActiveRangeList();
参数
名称 | 类型 | 说明 |
---|---|---|
rangeList | RangeList | 要选择的范围列表。 |
返程
RangeList
- 新选择的范围列表
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setActiveSelection(range)
设置此工作表的有效选择区域。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("A1:D4"); sheet.setActiveSelection(range);
参数
名称 | 类型 | 说明 |
---|---|---|
range | Range | 要设为有效所选范围的范围。 |
返程
Range
- 新的有效范围
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setActiveSelection(a1Notation)
按照 A1 或 R1C1 表示法设置有效的选择。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; sheet.setActiveSelection("A1:D4");
参数
名称 | 类型 | 说明 |
---|---|---|
a1Notation | String | 要设置为有效的范围,以 A1 表示法或 R1C1 表示法指定。 |
返程
Range
- 新的有效范围
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setActiveSheet(sheet)
将指定工作表设置为电子表格中的活动工作表。Google 表格界面会显示所选的工作表,除非该工作表属于其他电子表格。
// The code below makes the first sheet active in the active spreadsheet. var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); spreadsheet.setActiveSheet(spreadsheet.getSheets()[0]);
参数
名称 | 类型 | 说明 |
---|---|---|
sheet | Sheet | 要设为活动工作表的工作表。 |
返程
Sheet
- 活动工作表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setActiveSheet(sheet, restoreSelection)
将给定工作表设置为电子表格中的活动工作表,并提供用于恢复该工作表中最近选择的内容的选项。Google 表格界面会显示所选的工作表,除非该工作表属于其他电子表格。
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var firstSheet = spreadsheet.getSheets()[0]; var secondSheet = spreadsheet.getSheets()[1]; // Set the first sheet as the active sheet and select the range D4:F4. spreadsheet.setActiveSheet(firstSheet).getRange('D4:F4').activate(); // Switch to the second sheet to do some work. spreadsheet.setActiveSheet(secondSheet); // Switch back to first sheet, and restore its selection. spreadsheet.setActiveSheet(firstSheet, true); // The selection of first sheet is restored, and it logs D4:F4 var range = spreadsheet.getActiveSheet().getSelection().getActiveRange(); Logger.log(range.getA1Notation());
参数
名称 | 类型 | 说明 |
---|---|---|
sheet | Sheet | 新的活动工作表。 |
restoreSelection | Boolean | Tf true ,当新工作表变为活动状态时,系统会再次选择最近选中的新工作表;如果设置为 false ,则新工作表将变为活动状态,而不会改变当前选定的内容。 |
返程
Sheet
- 新的活动工作表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setColumnWidth(columnPosition, width)
设置指定列的宽度(以像素为单位)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Sets the first column to a width of 200 pixels sheet.setColumnWidth(1, 200);
参数
名称 | 类型 | 说明 |
---|---|---|
columnPosition | Integer | 要设置的指定列的位置。 |
width | Integer | 要设置的宽度(以像素为单位)。 |
返程
Sheet
- 工作表,可用于方法链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setCurrentCell(cell)
将指定单元格设置为 current cell
。
如果指定的单元格位于已选择的范围内,则该范围会变为活动范围,并且该单元格是当前单元格。
如果任何选定范围内都不存在指定的单元格,则系统会移除任何现有的选择,并且此单元格将成为当前单元格和活动范围。
注意:指定的 Range
必须包含一个单元格,否则会抛出异常。
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var cell = sheet.getRange('B5'); sheet.setCurrentCell(cell); var selection = sheet.getSelection(); // Current cell: B5 var currentCell = selection.getCurrentCell();
参数
名称 | 类型 | 说明 |
---|---|---|
cell | Range | 要设为当前单元格的单元格。 |
返程
Range
- 新设置的当前单元格
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFrozenColumns(columns)
冻结指定数量的列。如果为零,则不会冻结任何列。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Freezes the first column sheet.setFrozenColumns(1);
参数
名称 | 类型 | 说明 |
---|---|---|
columns | Integer | 要冻结的列数。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFrozenRows(rows)
冻结指定的行数。如果为零,则没有冻结任何行。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Freezes the first row sheet.setFrozenRows(1);
参数
名称 | 类型 | 说明 |
---|---|---|
rows | Integer | 要冻结的行数。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setIterativeCalculationConvergenceThreshold(minThreshold)
设置迭代计算的最小阈值。当连续计算的结果差异小于此值时,迭代计算停止。此值必须是非负数,默认为 0.05。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Sets the iterative calculation convergence threshold for the spreadsheet. ss.setIterativeCalculationConvergenceThreshold(2); // Logs the threshold to the console. console.log(ss.getIterativeCalculationConvergenceThreshold());
参数
名称 | 类型 | 说明 |
---|---|---|
minThreshold | Number | 最小收敛阈值(必须为非负数)。 |
返程
Spreadsheet
- 此电子表格,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setIterativeCalculationEnabled(isEnabled)
设置是否在此电子表格中启用迭代计算。如果在启用计算时之前未设置最大计算周期数和收敛阈值,则它们默认分别为 50 和 0.05。如果之前设置过这两者,它们会保留之前的值。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Activates iterative calculation on the spreadsheet. ss.setIterativeCalculationEnabled(true); // Logs whether iterative calculation is activated for the spreadsheet. console.log(ss.isIterativeCalculationEnabled());
参数
名称 | 类型 | 说明 |
---|---|---|
isEnabled | Boolean | 如果应启用迭代计算,则为 true ;否则为 false 。 |
返程
Spreadsheet
- 此电子表格,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setMaxIterativeCalculationCycles(maxIterations)
设置迭代计算期间应该执行的最大计算迭代次数。此值必须介于 1 到 10,000(含)之间,默认值为 50。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Sets the max iterative calculation cycles for the spreadsheet. ss.setMaxIterativeCalculationCycles(10); // Logs the max iterative calculation cycles to the console. console.log(ss.getMaxIterativeCalculationCycles());
参数
名称 | 类型 | 说明 |
---|---|---|
maxIterations | Integer | 计算迭代次数上限(介于 1 到 10,000 之间)。 |
返程
Spreadsheet
- 此电子表格,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setNamedRange(name, range)
为范围命名。
// The code below creates a new named range "TaxRates" in the active spreadsheet var ss = SpreadsheetApp.getActiveSpreadsheet(); ss.setNamedRange("TaxRates", SpreadsheetApp.getActiveRange());
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 为范围指定的名称。 |
range | Range | 范围规范。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setRecalculationInterval(recalculationInterval)
设置此电子表格应重新计算的频率。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Sets the calculation interval for the spreadsheet to 'ON_CHANGE'. const interval = ss.setRecalculationInterval(SpreadsheetApp.RecalculationInterval.ON_CHANGE); // Logs the calculation interval to the console. console.log(interval);
参数
名称 | 类型 | 说明 |
---|---|---|
recalculationInterval | RecalculationInterval | 新的重新计算间隔。 |
返程
Spreadsheet
- 此电子表格,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setRowHeight(rowPosition, height)
设置指定行的行高(以像素为单位)。默认情况下,行会增大以适应单元格的内容。如果要将行强制设置为指定高度,请使用 Sheet.setRowHeightsForced(startRow, numRows, height)
。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Sets the first row to a height of 200 pixels sheet.setRowHeight(1, 200);
参数
名称 | 类型 | 说明 |
---|---|---|
rowPosition | Integer | 要更改的行位置。 |
height | Integer | 要设置的高度(以像素为单位)。 |
返程
Sheet
- 工作表,可用于方法链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setSpreadsheetLocale(locale)
设置电子表格的语言区域。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Sets the spreadsheet locale. ss.setSpreadsheetLocale('fr'); // Gets the spreadsheet locale. const ssLocale = ss.getSpreadsheetLocale(); // Logs the locale to the console. console.log(ssLocale);
参数
名称 | 类型 | 说明 |
---|---|---|
locale | String | 要使用的语言区域代码(例如“en”“fr”或“en_US”)。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setSpreadsheetTheme(theme)
在电子表格上设置主题背景。
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // The code below sets the second predefined theme as the current theme of the spreadsheet. var predefinedThemesList = spreadsheet.getPredefinedSpreadsheetThemes(); spreadsheet.setSpreadsheetTheme(predefinedThemesList[1]);
参数
名称 | 类型 | 说明 |
---|---|---|
theme | SpreadsheetTheme | 要应用的主题。 |
返程
SpreadsheetTheme
- 新的当前主题。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setSpreadsheetTimeZone(timezone)
设置电子表格的时区。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Sets the time zone of the spreadsheet. ss.setSpreadsheetTimeZone('America/New_York'); // Gets the time zone of the spreadsheet. const ssTimeZone = ss.getSpreadsheetTimeZone(); // Logs the time zone to the console. console.log(ssTimeZone);
参数
名称 | 类型 | 说明 |
---|---|---|
timezone | String | 时区,以“long”格式指定(例如,Joda.org 中列出的“America/New_York”)。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
show(userInterface)
以用户浏览器视口为中心的对话框中显示自定义界面组件。服务器端脚本的执行不会暂停。为了与服务器端通信,界面组件必须对服务器端脚本进行异步回调。
如果服务器端脚本之前显示了尚未关闭的对话框,则系统会将现有对话框替换为新请求的对话框的界面。
以下代码段将在具有指定标题、高度和宽度的对话框中显示一个简单的 HtmlService
应用:
var htmlApp = HtmlService .createHtmlOutput('<p>A change of speed, a change of style...</p>') .setTitle('My HtmlService Application') .setWidth(250) .setHeight(300); SpreadsheetApp.getActiveSpreadsheet().show(htmlApp); // The script resumes execution immediately after showing the dialog.
参数
名称 | 类型 | 说明 |
---|---|---|
userInterface | Object | HtmlOutput 。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/script.container.ui
sort(columnPosition)
按列对工作表进行升序排序。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Sorts the sheet by the first column, ascending sheet.sort(1);
参数
名称 | 类型 | 说明 |
---|---|---|
columnPosition | Integer | 要作为排序依据的列。 |
返程
Sheet
- 工作表,可用于方法链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
sort(columnPosition, ascending)
按列对工作表进行排序。接受一个参数来指定升序或降序。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // Sorts the sheet by the first column, descending sheet.sort(1, false);
参数
名称 | 类型 | 说明 |
---|---|---|
columnPosition | Integer | 要作为排序依据的列。 |
ascending | Boolean | true 表示升序,false 表示降序。 |
返程
Sheet
- 工作表,可用于方法链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
toast(msg)
在电子表格的右下角显示一个弹出式窗口,其中包含指定消息。
// Show a popup with the message "Task started". SpreadsheetApp.getActiveSpreadsheet().toast('Task started');
参数
名称 | 类型 | 说明 |
---|---|---|
msg | String | 要在消息框中显示的消息。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
toast(msg, title)
在电子表格的右下角显示一个弹出式窗口,其中包含给定的消息和标题。
// Show a popup with the title "Status" and the message "Task started". SpreadsheetApp.getActiveSpreadsheet().toast('Task started', 'Status');
参数
名称 | 类型 | 说明 |
---|---|---|
msg | String | 要在消息框中显示的消息。 |
title | String | 消息框的可选标题。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
toast(msg, title, timeoutSeconds)
在电子表格的右下角显示一个弹出式窗口,其中包含给定标题和消息,该窗口会在一段时间内保持可见状态。
// Show a 3-second popup with the title "Status" and the message "Task started". SpreadsheetApp.getActiveSpreadsheet().toast('Task started', 'Status', 3);
参数
名称 | 类型 | 说明 |
---|---|---|
msg | String | 要在消息框中显示的消息。 |
title | String | 消息框的可选标题。 |
timeoutSeconds | Number | 超时时间(以秒为单位);如果为 null ,消息框默认为 5 秒;如果为负值,消息框会一直保留,直到关闭为止。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
unhideColumn(column)
取消隐藏给定范围内的列。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This unhides the first column if it was previously hidden var range = sheet.getRange("A1"); sheet.unhideColumn(range);
参数
名称 | 类型 | 说明 |
---|---|---|
column | Range | 要取消隐藏的范围(如果已隐藏)。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
unhideRow(row)
取消隐藏给定范围内的行。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This unhides the first row if it was previously hidden var range = sheet.getRange("A1"); sheet.unhideRow(range);
参数
名称 | 类型 | 说明 |
---|---|---|
row | Range | 要取消隐藏的范围(如果已隐藏)。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
updateMenu(name, subMenus)
更新由 addMenu(name, subMenus)
添加的菜单。运作方式与 addMenu(name, subMenus)
完全相同。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var menuEntries = []; menuEntries.push({name: "Lone Menu Entry", functionName: "function1"}); ss.updateMenu("addMenuExample", menuEntries);
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 要更新的菜单的名称。 |
subMenus | Object[] | 包含 name 和 functionName
参数的 JavaScript 映射数组。您可以使用所含库中的函数,例如
Library.libFunction1 。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
waitForAllDataExecutionsCompletion(timeoutInSeconds)
等待电子表格中的所有当前执行任务完成,在提供的秒数后超时。如果超时,虽然执行未完成,但并未取消数据执行,则会抛出异常。
参数
名称 | 类型 | 说明 |
---|---|---|
timeoutInSeconds | Integer | 等待数据执行的时间(以秒为单位)。最大值为 300 秒。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets