访问和修改电子表格范围。范围可以是工作表中的单个单元格,也可以是工作表中一组相邻的单元格。
方法
详细文档
activate()
将指定范围设置为 active range
,并将范围中的左上角单元格设置为 current cell
。
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; var range = sheet.getRange('A1:D10'); range.activate(); var selection = sheet.getSelection(); // Current cell: A1 var currentCell = selection.getCurrentCell(); // Active Range: A1:D10 var activeRange = selection.getActiveRange();
返程
Range
- 此范围用于串联。
activateAsCurrentCell()
将指定的单元格设置为 current cell
。
如果指定的单元格存在于现有范围内,则此范围会变为有效范围,单元格为当前单元格。
如果指定的单元格不在任何现有范围内,则系统会移除现有选择,使单元格成为当前单元格和活动范围。
注意:指定的 Range
必须由一个单元格组成,否则会抛出异常。
// Gets the first sheet of the spreadsheet. var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; // Gets the cell B5 and sets it as the active cell. var range = sheet.getRange('B5'); var currentCell = range.activateAsCurrentCell(); // Logs the activated cell. console.log(currentCell.getA1Notation());
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
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'); // Gets Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets row 2 on the sheet. const range = sheet.getRange('2:2'); // Adds the key 'NAME' to the developer metadata for row 2. range.addDeveloperMetadata('NAME'); // Gets the metadata and logs it to the console. const developerMetaData = range.getDeveloperMetadata()[0]; console.log(developerMetaData.getKey());
参数
名称 | 类型 | 说明 |
---|---|---|
key | String | 新开发者元数据的键。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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'); // Gets Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets row 2 on Sheet1. const range = sheet.getRange('2:2'); // Adds the key 'NAME' and sets the developer metadata visibility to 'DOCUMENT' // for row 2 on Sheet1. range.addDeveloperMetadata('NAME', SpreadsheetApp.DeveloperMetadataVisibility.DOCUMENT); // Gets the updated metadata info and logs it to the console. const developerMetaData = range.getDeveloperMetadata()[0]; console.log(developerMetaData.getKey()); console.log(developerMetaData.getVisibility().toString());
参数
名称 | 类型 | 说明 |
---|---|---|
key | String | 新开发者元数据的键。 |
visibility | DeveloperMetadataVisibility | 新开发者元数据的公开范围。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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'); // Gets Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets row 2 of Sheet1. const range = sheet.getRange('2:2'); // Adds the key 'NAME' and sets the value to 'GOOGLE' for the metadata of row 2. range.addDeveloperMetadata('NAME', 'GOOGLE'); // Gets the metadata and logs it to the console. const developerMetaData = range.getDeveloperMetadata()[0]; console.log(developerMetaData.getKey()); console.log(developerMetaData.getValue());
参数
名称 | 类型 | 说明 |
---|---|---|
key | String | 新开发者元数据的键。 |
value | String | 新开发者元数据的值。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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'); // Gets Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets row 2 on the sheet. const range = sheet.getRange('2:2'); // Adds the key 'NAME', sets the value to 'GOOGLE', and sets the visibility // to PROJECT for row 2 on the sheet. range.addDeveloperMetadata( 'NAME', 'GOOGLE', SpreadsheetApp.DeveloperMetadataVisibility.PROJECT); // Gets the updated metadata info and logs it to the console. const developerMetaData = range.getDeveloperMetadata()[0]; console.log(developerMetaData.getKey()); console.log(developerMetaData.getValue()); console.log(developerMetaData.getVisibility().toString());
参数
名称 | 类型 | 说明 |
---|---|---|
key | String | 新开发者元数据的键。 |
value | String | 新开发者元数据的值。 |
visibility | DeveloperMetadataVisibility | 新开发者元数据的公开范围。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
applyColumnBanding()
将范围的默认条带主题应用于范围。默认情况下,该条带有页眉没有页脚颜色。
// 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 Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets row 2 on the sheet. const range = sheet.getRange('2:2'); // Applies column banding to row 2. const colBanding = range.applyColumnBanding(); // Gets the first banding on the sheet and logs the color of the header column. console.log(sheet.getBandings()[0].getHeaderColumnColorObject().asRgbColor().asHexString()); // Gets the first banding on the sheet and logs the color of the second column. console.log(sheet.getBandings()[0].getSecondColumnColorObject().asRgbColor().asHexString());
返程
Banding
- 新的带。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
applyColumnBanding(bandingTheme)
将指定的列带主题应用于该范围。默认情况下,该条带有页眉没有页脚颜色。
// 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 Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets row 2 on the sheet. const range = sheet.getRange('2:2'); // Applies the INDIGO color banding theme to the columns in row 2. const colBanding = range.applyColumnBanding(SpreadsheetApp.BandingTheme.INDIGO); // Gets the first banding on the sheet and logs the color of the second column. console.log(sheet.getBandings()[0].getSecondColumnColorObject().asRgbColor().asHexString());
参数
名称 | 类型 | 说明 |
---|---|---|
bandingTheme | BandingTheme | 要应用于范围中的列的颜色主题。 |
返程
Banding
- 新的带。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
applyColumnBanding(bandingTheme, showHeader, showFooter)
将指定的列带主题应用于具有指定页眉和页脚设置的范围。
// 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 Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets rows 12-22 on the sheet. const range = sheet.getRange('12:22'); // Applies the BLUE color banding theme to rows 12-22. // Sets the header visibility to false and the footer visibility to true. const colBanding = range.applyColumnBanding(SpreadsheetApp.BandingTheme.BLUE, false, true); // Gets the banding color and logs it to the console. console.log(sheet.getBandings()[0].getSecondColumnColorObject().asRgbColor().asHexString()); // Gets the header color object and logs it to the console. Returns null because the header // visibility is set to false. console.log(sheet.getBandings()[0].getHeaderColumnColorObject()); // Gets the footer color and logs it to the console. console.log(sheet.getBandings()[0].getFooterColumnColorObject().asRgbColor().asHexString());
参数
名称 | 类型 | 说明 |
---|---|---|
bandingTheme | BandingTheme | 要应用于范围中的列的颜色主题。 |
showHeader | Boolean | 如果为 true ,则系统会将频段主题标题颜色应用于第一列。 |
showFooter | Boolean | 如果为 true ,系统会为最后一列应用条带主题页脚颜色。 |
返程
Banding
- 新的带。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
applyRowBanding()
applyRowBanding(bandingTheme)
将指定的行带主题应用于该范围。默认情况下,该条带有页眉没有页脚颜色。
参数
名称 | 类型 | 说明 |
---|---|---|
bandingTheme | BandingTheme | 要应用于范围中各行的颜色主题。 |
返程
Banding
- 新的带。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
applyRowBanding(bandingTheme, showHeader, showFooter)
将指定的行带主题应用于具有指定页眉和页脚设置的范围。
参数
名称 | 类型 | 说明 |
---|---|---|
bandingTheme | BandingTheme | 要应用于范围中各行的颜色主题。 |
showHeader | Boolean | 如果为 true ,系统会为第一行应用条带主题标题颜色。 |
showFooter | Boolean | 如果值为 true ,则系统会将频段主题页脚颜色应用于最后一行。 |
返程
Banding
- 新的带。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
autoFill(destination, series)
根据此范围内的数据填充 destinationRange
。新值也由指定的 series
类型确定。目标范围必须包含此范围,并仅向一个方向扩展。例如,以下代码会根据 A1:A4
中的当前值用一系列递增的数字填充 A1:A20
:
var sheet = SpreadsheetApp.getActiveSheet(); // Has values [1, 2, 3, 4]. var sourceRange = sheet.getRange("A1:A4"); // The range to fill with values. var destination = sheet.getRange("A1:A20"); // Inserts new values in A5:A20, continuing the pattern expressed in A1:A4 sourceRange.autoFill(destination, SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
参数
名称 | 类型 | 说明 |
---|---|---|
destination | Range | 要自动填入值的范围。目标范围应包含此范围,并且仅朝一个方向(向上、向下、向左或向右)扩展。 |
series | AutoFillSeries | 用于计算新值的自动填充系列类型。该系列的影响因来源数据的类型和数量而异。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
autoFillToNeighbor(series)
根据相邻的单元格计算填充新数据的范围,并根据该范围中包含的数据自动使用新值填充该范围。这些新值也由指定的 series
类型确定。
计算的目的地范围会考虑周围数据以确定应插入新值的位置:如果正在自动填充的列的紧邻左侧或右侧具有数据,则新值将只延伸至此相邻数据。
例如,如果 A1:A20
填充了一系列递增数字,则针对包含一系列日期的 B1:B4
范围调用此方法,且新值仅插入 B5:B20
。这样一来,这些新值就会“保留”到包含 A 列中值的单元格。
var sheet = SpreadsheetApp.getActiveSheet(); // A1:A20 has values [1, 2, 3, ... 20]. // B1:B4 has values [1/1/2017, 1/2/2017, ...] var sourceRange = sheet.getRange("B1:B4"); // Results in B5:B20 having values [1/5/2017, ... 1/20/2017] sourceRange.autoFillToNeighbor(SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
参数
名称 | 类型 | 说明 |
---|---|---|
series | AutoFillSeries | 用于计算新值的自动填充系列类型。该系列的影响因来源数据的类型和数量而异。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
breakApart()
canEdit()
确定用户是否有权修改范围中的每个单元格。电子表格所有者始终可以修改受保护的范围和工作表。
返程
Boolean
- 如果用户有权修改范围中的每个单元格,则为 true
;否则为 false
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
check()
将复选框中的状态更改为“已选中”。忽略该范围内当前未配置已勾选或未取消选中值的单元格。
// Changes the state of cells which currently contain either the checked or unchecked value // configured in the range A1:B10 to 'checked'. var range = SpreadsheetApp.getActive().getRange('A1:B10'); range.check();
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
clear()
clear(options)
根据指定的高级选项,清除内容范围、格式、数据验证规则和/或注释。默认情况下,所有数据都会被清除。
// The code below clears range C2:G8 in the active sheet, but preserves the format, // data validation rules, and comments. SpreadsheetApp.getActiveSheet().getRange(2, 3, 6, 5).clear({contentsOnly: true});
参数
名称 | 类型 | 说明 |
---|---|---|
options | Object | 一个 JavaScript 对象,用于指定高级参数,如下所列。 |
高级参数
名称 | 类型 | 说明 |
---|---|---|
commentsOnly | Boolean | 是否仅清除评论。 |
contentsOnly | Boolean | 是否仅清除内容。 |
formatOnly | Boolean | 是否仅清除格式;请注意,清除格式也会清除数据验证规则。 |
validationsOnly | Boolean | 是否仅清除数据验证规则。 |
skipFilteredRows | Boolean | 是否避免清除被滤除的行。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
clearContent()
清除范围的内容,保持格式不变。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("A1:D10"); range.clearContent();
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
clearDataValidations()
清除范围的数据验证规则。
// Clear the data validation rules for cells A1:B5. var range = SpreadsheetApp.getActive().getRange('A1:B5'); range.clearDataValidations();
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
clearFormat()
清除此范围的格式。
这会清除范围中一个或多个单元格的文本格式,但不会重置任何数字格式设置规则。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("A1:D10"); range.clearFormat();
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
clearNote()
collapseGroups()
收起整个范围内的所有组。如果任何组都未完全在该范围内,则收起部分范围内的最深的展开组。
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; var range = sheet.getActiveRange(); // All row and column groups within the range are collapsed. range.collapseGroups();
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
copyFormatToRange(gridId, column, columnEnd, row, rowEnd)
将范围格式复制到指定位置。如果目标位置大于或小于来源范围,则相应来源会相应地重复或被截断。请注意,此方法仅复制格式。
如需详细了解 gridId 参数,请参阅 getGridId()
。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var source = ss.getSheets()[0]; var range = source.getRange("B2:D4"); // This copies the formatting in B2:D4 in the source sheet to // D4:F6 in the sheet with gridId 1555299895. Note that you can get the gridId // of a sheet by calling sheet.getSheetId() or range.getGridId(). range.copyFormatToRange(1555299895, 4, 6, 4, 6);
参数
名称 | 类型 | 说明 |
---|---|---|
gridId | Integer | 电子表格中的唯一工作表 ID(无论排名如何)。 |
column | Integer | 目标范围的第一列。 |
columnEnd | Integer | 目标范围的结束列。 |
row | Integer | 目标范围的起始行。 |
rowEnd | Integer | 目标范围的结束行。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
另请参阅
copyFormatToRange(sheet, column, columnEnd, row, rowEnd)
将范围格式复制到指定位置。如果目标位置大于或小于来源范围,则相应来源会相应地重复或被截断。请注意,此方法仅复制格式。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var source = ss.getSheets()[0]; var destination = ss.getSheets()[1]; var range = source.getRange("B2:D4"); // This copies the formatting in B2:D4 in the source sheet to // D4:F6 in the second sheet range.copyFormatToRange(destination, 4, 6, 4, 6);
参数
名称 | 类型 | 说明 |
---|---|---|
sheet | Sheet | 目标工作表。 |
column | Integer | 目标范围的第一列。 |
columnEnd | Integer | 目标范围的结束列。 |
row | Integer | 目标范围的起始行。 |
rowEnd | Integer | 目标范围的结束行。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
copyTo(destination)
将一个单元格范围的数据复制到另一个单元格范围。值和格式均会被复制。
// The code below copies the first 5 columns over to the 6th column. var sheet = SpreadsheetApp.getActiveSheet(); var rangeToCopy = sheet.getRange(1, 1, sheet.getMaxRows(), 5); rangeToCopy.copyTo(sheet.getRange(1, 6));
参数
名称 | 类型 | 说明 |
---|---|---|
destination | Range | 要复制到的目标范围;仅左上角单元格位置相关。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
copyTo(destination, copyPasteType, transposed)
将一个单元格范围的数据复制到另一个单元格范围。
// The code below copies only the values of the first 5 columns over to the 6th column. var sheet = SpreadsheetApp.getActiveSheet(); sheet.getRange("A:E").copyTo(sheet.getRange("F1"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
参数
名称 | 类型 | 说明 |
---|---|---|
destination | Range | 要复制到的目标范围;仅左上角单元格位置相关。 |
copyPasteType | CopyPasteType | 指定如何将范围内容粘贴到目的地的类型。 |
transposed | Boolean | 是否应以粘贴状态的方向粘贴范围。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
copyTo(destination, options)
将一个单元格范围的数据复制到另一个单元格范围。默认情况下,值和格式设置会被复制,但可以使用高级参数替换。
// The code below copies only the values of the first 5 columns over to the 6th column. var sheet = SpreadsheetApp.getActiveSheet(); sheet.getRange("A:E").copyTo(sheet.getRange("F1"), {contentsOnly:true});
参数
名称 | 类型 | 说明 |
---|---|---|
destination | Range | 要复制到的目标范围;仅左上角单元格位置相关。 |
options | Object | 一个 JavaScript 对象,用于指定高级参数,如下所列。 |
高级参数
名称 | 类型 | 说明 |
---|---|---|
formatOnly | Boolean | 指明只应复制该格式 |
contentsOnly | Boolean | 指明只应复制内容 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
copyValuesToRange(gridId, column, columnEnd, row, rowEnd)
将范围的内容复制到指定位置。如果目标位置大于或小于来源范围,则相应来源会相应地重复或被截断。
如需详细了解 gridId 参数,请参阅 getGridId()
。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var source = ss.getSheets()[0]; var range = source.getRange("B2:D4"); // This copies the data in B2:D4 in the source sheet to // D4:F6 in the sheet with gridId 0 range.copyValuesToRange(0, 4, 6, 4, 6);
参数
名称 | 类型 | 说明 |
---|---|---|
gridId | Integer | 电子表格中的唯一工作表 ID(无论排名如何)。 |
column | Integer | 目标范围的第一列。 |
columnEnd | Integer | 目标范围的结束列。 |
row | Integer | 目标范围的起始行。 |
rowEnd | Integer | 目标范围的结束行。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
另请参阅
copyValuesToRange(sheet, column, columnEnd, row, rowEnd)
将范围的内容复制到指定位置。如果目标位置大于或小于来源范围,则相应来源会相应地重复或被截断。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var source = ss.getSheets()[0]; var destination = ss.getSheets()[1]; var range = source.getRange("B2:D4"); // This copies the data in B2:D4 in the source sheet to // D4:F6 in the second sheet range.copyValuesToRange(destination, 4, 6, 4, 6);
参数
名称 | 类型 | 说明 |
---|---|---|
sheet | Sheet | 目标工作表。 |
column | Integer | 目标范围的第一列。 |
columnEnd | Integer | 目标范围的结束列。 |
row | Integer | 目标范围的起始行。 |
rowEnd | Integer | 目标范围的结束行。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
createDataSourcePivotTable(dataSource)
根据数据源创建空数据源数据透视表,锚定为此范围内第一个单元格。
此示例展示了如何创建和配置新的数据源数据透视表。
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var anchorCell = spreadsheet.getSheets()[0].getRange('A1'); var dataSource = spreadsheet.getDataSources()[0]; var pivotTable = anchorCell.createDataSourcePivotTable(dataSource); pivotTable.addRowGroup('dataColumnA'); pivotTable.addColumnGroup('dataColumnB'); pivotTable.addPivotValue('dataColumnC', SpreadsheetApp.PivotTableSummarizeFunction.SUM); pivotTable.addFilter('dataColumnA', SpreadsheetApp.newFilterCriteria().whenTextStartsWith('A').build());
参数
名称 | 类型 | 说明 |
---|---|---|
dataSource | DataSource | 用于创建数据透视表的数据源。 |
返程
DataSourcePivotTable
- 新创建的数据源数据透视表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
createDataSourceTable(dataSource)
根据数据源创建空的数据源表,将表固定为此范围内的第一个单元格。
此示例展示了如何创建和配置新的数据源表。
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var anchorCell = spreadsheet.getSheets()[0].getRange('A1'); var dataSource = spreadsheet.getDataSources()[0]; var dataSourceTable = anchorCell.createDataSourceTable(dataSource); .addColumns('dataColumnA', 'dataColumnB', 'dataColumnC') .addSortSpec('dataColumnA', /* ascending= *\/ true) .addSortSpec('dataColumnB', /* ascending= *\/ false);
参数
名称 | 类型 | 说明 |
---|---|---|
dataSource | DataSource | 用于创建数据透视表的数据源。 |
返程
DataSourceTable
- 新创建的数据源表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
createDeveloperMetadataFinder()
返回 DeveloperMetadataFinderApi,用于查找此范围内的开发者元数据。仅当元数据完全在该范围内时,元数据才会在该范围内。例如,与行“3:3”相关的元数据不在“A1:D5”范围内,但属于“1:5”的范围。
// 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 Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets the range A1:C6. const range = sheet.getRange('A1:C6'); // Creates a developer metadata finder to search for metadata in the scope of this range. const developerMetaDataFinder = range.createDeveloperMetadataFinder(); // Logs information about the developer metadata finder to the console. const developerMetaData = developerMetaDataFinder.find()[0]; console.log(developerMetaData.getKey()); console.log(developerMetaData.getValue()); console.log(developerMetaData.getVisibility().toString());
返程
DeveloperMetadataFinder
- 用于搜索此范围内元数据的开发者元数据查找工具。
createFilter()
创建过滤器并将其应用于工作表中的指定范围。您不能在工作表上创建多个过滤器。如需在创建过滤器后对其进行访问和修改,请使用 getFilter()
或 Sheet.getFilter()
。
let ss = SpreadsheetApp.getActiveSheet(); let range = ss.getRange("A1:C20"); // Creates a new filter and applies it to the range A1:C20 on the active sheet. function createFilter() { range.createFilter(); } // Gets the filter and applies criteria that only shows cells that aren't empty. function getFilterAddCriteria() { let filter = range.getFilter(); let criteria = SpreadsheetApp.newFilterCriteria() .whenCellNotEmpty() .build(); filter.setColumnFilterCriteria(2, criteria); }可以使用此方法为
Grid
工作表(默认的工作表类型)创建过滤器。
网格工作表是指未连接到数据库的工作表。如需创建其他类型的过滤器,请参阅以下内容:- 使用
PivotTable.addFilter(sourceDataColumn, filterCriteria)
创建数据透视表过滤条件 - 使用
DataSourceSheet.addFilter(columnName, filterCriteria)
为连接到数据库的工作表创建过滤器 - 为使用
DataSourcePivotTable.addFilter(columnName, filterCriteria)
连接的数据库创建数据透视表过滤器
返程
Filter
- 新的过滤条件。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
createPivotTable(sourceData)
根据锚定于此范围内第一个单元格的指定 sourceData
创建空数据透视表。
// 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 Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets cell A1 as a range in order to place the pivot table. const range = sheet.getRange('A1'); // Gets the range of the source data for the pivot table. const dataRange = sheet.getRange('E12:G20'); // Creates an empty pivot table from the specified source data. const pivotTable = range.createPivotTable(dataRange); // Logs the values from the pivot table's source data to the console. console.log(pivotTable.getSourceDataRange().getValues());
参数
名称 | 类型 | 说明 |
---|---|---|
sourceData | Range | 要创建数据透视表的数据。 |
返程
PivotTable
- 新创建的 PivotTable
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
createTextFinder(findText)
为范围创建文本查找器,以便查找并替换此范围中的文本。
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; var range = sheet.getActiveRange(); // Creates a text finder for the range. var textFinder = range.createTextFinder('dog'); // Returns the first occurrence of 'dog'. 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
deleteCells(shiftDimension)
删除此范围的单元格。工作表中沿所提供的维度的现有数据会移动到已删除的范围。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("A1:D10"); range.deleteCells(SpreadsheetApp.Dimension.COLUMNS);
参数
名称 | 类型 | 说明 |
---|---|---|
shiftDimension | Dimension | 按现有维度偏移现有数据。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
expandGroups()
展开范围或控件切换开关可与该范围相交的收起组。控件切换位置是控件开关显示位置的索引,紧接在组之前或之后,具体取决于设置。如果同一位置有多个组,最宽松的组将展开。
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; var range = sheet.getActiveRange(); // All row and column groups within the range are expanded. range.expandGroups();
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getA1Notation()
返回范围的字符串说明(采用 A1 表示法)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange(1, 1, 2, 5); // Logs "A1:E2" Logger.log(range.getA1Notation());
返程
String
- 范围的说明(采用 A1 表示法)。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getBackground()
返回范围内左上角单元格的背景颜色(例如 '#ffffff'
)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B5"); Logger.log(cell.getBackground());
返程
String
- 背景的颜色代码。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getBackgroundObject()
返回范围内左上角单元格的背景颜色。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B5"); Logger.log(cell.getBackgroundObject().asRgbColor().asHexString());
返程
Color
- 左上角左上角单元格的背景颜色。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getBackgroundObjects()
返回范围内单元格的背景颜色。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B5:C6"); var bgColors = range.getBackgroundObjects(); for (var i in bgColors) { for (var j in bgColors[i]) { Logger.log(bgColors[i][j].asRgbColor().asHexString()); } }
返程
Color[][]
- 二维背景颜色数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getBackgrounds()
返回范围内单元格的背景颜色(例如 '#ffffff'
)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B5:C6"); var bgColors = range.getBackgrounds(); for (var i in bgColors) { for (var j in bgColors[i]) { Logger.log(bgColors[i][j]); } }
返程
String[][]
- 背景的二维颜色代码数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getBandings()
返回此范围内所有单元格应用的所有条带。
// 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 Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Sets a range. const range = sheet.getRange('A1:K50'); // Gets the banding info for the range. const bandings = range.getBandings(); // Logs the second row color for each banding to the console. for (let banding of bandings) { console.log(banding.getSecondRowColor()); }
返程
Banding[]
- 此范围中应用于任何单元格的所有条带。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getCell(row, column)
返回范围内的给定单元格。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); // The row and column here are relative to the range // getCell(1,1) in this code returns the cell at B2 var cell = range.getCell(1, 1); Logger.log(cell.getValue());
参数
名称 | 类型 | 说明 |
---|---|---|
row | Integer | 单元格所在范围的范围。 |
column | Integer | 单元格所代表的范围(相对于范围)。 |
返程
Range
- 一个范围,包含指定坐标处的单个单元格。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getColumn()
返回此范围的初始列位置。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); // Logs "2.0" Logger.log(range.getColumn());
返程
Integer
- 范围在电子表格中的起始列位置。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataRegion()
返回四个基数 Direction
中扩展的范围的副本,以覆盖所有包含数据的相邻单元格。如果该范围被空单元格(不含沿对角线的单元格)包围,则会返回该范围本身。这类似于在编辑器中选择范围并输入 Ctrl+A
。
// Assume the active spreadsheet is blank. var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; sheet.getRange("C2").setValue(100); sheet.getRange("B3").setValue(100); sheet.getRange("D3").setValue(100); sheet.getRange("C4").setValue(100); // Logs "B2:D4" Logger.log(sheet.getRange("C3").getDataRegion().getA1Notation());
返程
Range
- 范围的数据区域或整个电子表格的范围。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataRegion(dimension)
如果指定的维度为 Dimension.ROWS
,则返回范围扩展的 Direction.UP
和 Direction.DOWN
的副本;如果维度为 Dimension.COLUMNS
,则返回 Direction.NEXT
和 Direction.PREVIOUS
。该范围的扩展取决于检测范围,而该范围是组织表格形式的数据。扩展的范围涵盖所有相邻单元格,其中有沿指定维度的数据(包括表格边界)。如果原始范围沿指定维度的空白单元格周围,则返回范围本身。此方法类似于在编辑器中选择范围并针对列输入
Ctrl+Space
或针对行输入 Shift+Space
。
// Assume the active spreadsheet is blank. var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; sheet.getRange("C2").setValue(100); sheet.getRange("B3").setValue(100); sheet.getRange("D3").setValue(100); sheet.getRange("C4").setValue(100); // Logs "C2:C4" Logger.log(sheet.getRange("C3").getDataRegion(SpreadsheetApp.Dimension.ROWS).getA1Notation()); // Logs "B3:D3" Logger.log( sheet.getRange("C3").getDataRegion(SpreadsheetApp.Dimension.COLUMNS).getA1Notation());
参数
名称 | 类型 | 说明 |
---|---|---|
dimension | Dimension | 要扩大范围的维度。 |
返程
Range
- 范围的数据区域或范围,包含每列或每行所涵盖的原始范围。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataSourceFormula()
返回范围内第一个单元格的 DataSourceFormula
,如果单元格不包含数据源公式,则返回 null
。
返程
DataSourceFormula
- 单元格的 DataSourceFormula
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataSourceFormulas()
返回范围中单元格的 DataSourceFormula
。
返程
DataSourceFormula[]
- 一个 DataSourceFormula
数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataSourcePivotTables()
获取与范围重叠的所有数据源数据透视表。
返程
DataSourcePivotTable[]
- 数据源数据透视表的列表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataSourceTables()
获取与范围相关的所有数据源表。
返程
DataSourceTable[]
- 数据源表列表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataSourceUrl()
返回此范围内数据的网址,该网址可用于创建图表和查询。
Code.gs
function doGet() { var ss = SpreadsheetApp.openById('1khO6hBWTNNyvyyxvob7aoZTI9ZvlqqASNeq0e29Tw2c'); var sheet = ss.getSheetByName('ContinentData'); var range = sheet.getRange('A1:B8'); var template = HtmlService.createTemplateFromFile('piechart'); template.dataSourceUrl = range.getDataSourceUrl(); return template.evaluate(); }
piechart.html
<!DOCTYPE html> <html> <head> <!--Load the AJAX API--> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript"> // Load the Visualization API and the corechart package. google.charts.load('current', {'packages': ['corechart']}); // Set a callback to run when the Google Visualization API is loaded. google.charts.setOnLoadCallback(queryData); function queryData() { var query = new google.visualization.Query('<?= dataSourceUrl ?>'); query.send(drawChart); } // Callback that creates and populates a data table, // instantiates the pie chart, passes in the data and // draws it. function drawChart(response) { if (response.isError()) { alert('Error: ' + response.getMessage() + ' ' + response.getDetailedMessage()); return; } var data = response.getDataTable(); // Set chart options. var options = { title: 'Population by Continent', width: 400, height: 300 }; // Instantiate and draw the chart, passing in some options. var chart = new google.visualization.PieChart(document.getElementById('chart_div')); chart.draw(data, options); } </script> </head> <body> <!-- Div that holds the pie chart. --> <div id="chart_div"></div> </body> </html>
返程
String
- 此范围的网址,可作为可传递给其他 API(例如图表)的数据源。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataTable()
getDataTable(firstRowIsHeader)
以 DataTable 形式返回此范围内的数据。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("A1:B7"); // Calling this method with "true" sets the first line to be the title of the axes var datatable = range.getDataTable(true); // Note that this doesn't build an EmbeddedChart, so you can't just use // Sheet#insertChart(). To do that, use sheet.newChart().addRange() instead. var chart = Charts.newBarChart() .setDataTable(datatable) .setOption("title", "Your Title Here") .build();
参数
名称 | 类型 | 说明 |
---|---|---|
firstRowIsHeader | Boolean | 是否将第一行视为标题。 |
返程
DataTable
- 作为数据表的数据。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataValidation()
返回范围内左上角单元格的数据验证规则。如果未在单元格上设置数据验证,此方法将返回 null
。
// Log information about the data validation rule for cell A1. var cell = SpreadsheetApp.getActive().getRange('A1'); var rule = cell.getDataValidation(); if (rule != null) { var criteria = rule.getCriteriaType(); var args = rule.getCriteriaValues(); Logger.log('The data validation rule is %s %s', criteria, args); } else { Logger.log('The cell does not have a data validation rule.') }
返程
DataValidation
- 范围左上角的单元格的数据验证规则。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDataValidations()
返回范围内所有单元格的数据验证规则。如果给定单元格上尚未设置数据验证,此方法会针对该单元格在数组中的位置返回 null
。
// Change existing data validation rules that require a date in 2013 to require a date in 2014. var oldDates = [new Date('1/1/2013'), new Date('12/31/2013')]; var newDates = [new Date('1/1/2014'), new Date('12/31/2014')]; var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()); var rules = range.getDataValidations(); for (var i = 0; i < rules.length; i++) { for (var j = 0; j < rules[i].length; j++) { var rule = rules[i][j]; if (rule != null) { var criteria = rule.getCriteriaType(); var args = rule.getCriteriaValues(); if (criteria == SpreadsheetApp.DataValidationCriteria.DATE_BETWEEN && args[0].getTime() == oldDates[0].getTime() && args[1].getTime() == oldDates[1].getTime()) { // Create a builder from the existing rule, then change the dates. rules[i][j] = rule.copy().withCriteria(criteria, newDates).build(); } } } } range.setDataValidations(rules);
返程
DataValidation[][]
- 与范围中的单元格相关联的数据验证规则的二维数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDeveloperMetadata()
获取与此范围相关联的开发者元数据。
返程
DeveloperMetadata[]
- 与此范围相关联的开发者元数据。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDisplayValue()
返回范围内左上角单元格的显示值。值为 String
。
显示的值会考虑日期、时间和货币格式设置,包括电子表格的语言区域设置自动应用的格式。空单元格会返回一个空字符串。
// 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 Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets cell A30 and sets its value to 'Test code.' const cell = sheet.getRange('A30'); cell.setValue('Test code'); // Gets the value and logs it to the console. console.log(cell.getDisplayValue());
返程
String
- 此单元格内显示的值。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDisplayValues()
返回此范围值的矩形网格。
返回显示值的二维数组,这些值按行编入索引,然后按列。值为 String
对象。显示的值会考虑日期、时间和货币格式,包括电子表格的语言区域设置自动应用的格式。空单元格由数组中的空字符串表示。请注意,虽然范围索引从 1, 1
开始,但 JavaScript 数组是从 [0][0]
编入索引的。
// The code below gets the displayed values for the range C2:G8 // in the active spreadsheet. Note that this is a JavaScript array. var values = SpreadsheetApp.getActiveSheet().getRange(2, 3, 6, 4).getDisplayValues(); Logger.log(values[0][0]);
返程
String[][]
- 一个二维值数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFilter()
返回此范围所属工作表的过滤条件;如果工作表上没有过滤条件,则返回 null
。
let ss = SpreadsheetApp.getActiveSheet(); let range = ss.getRange("A1:C20"); // Gets the existing filter on the sheet that the given range belongs to. let filter = range.getFilter();
返程
Filter
- 过滤条件。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFontColorObject()
返回单元格左上角相应范围的字体颜色。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); Logger.log(range.getFontColorObject().asRgbColor().asHexString());
返程
Color
- 左上角单元格的字体颜色。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFontColorObjects()
返回范围内单元格的字体颜色。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); var results = range.getFontColorObjects(); for (var i in results) { for (var j in results[i]) { Logger.log(results[i][j].asRgbColor().asHexString()); } }
返程
Color[][]
- 与范围中的单元格相关联的字体颜色的二维数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFontFamilies()
返回范围内单元格的字体系列。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); var results = range.getFontFamilies(); for (var i in results) { for (var j in results[i]) { Logger.log(results[i][j]); } }
返程
String[][]
- 与范围中的单元格相关联的字体系列的二维数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFontFamily()
返回范围左上角的单元格的字体系列。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); Logger.log(range.getFontFamily());
返程
String
- 单元格的字体系列。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFontLine()
获取范围左上角的单元格线条样式('underline'
、'line-through'
或 'none'
)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); Logger.log(range.getFontLine());
返程
String
- 字体行。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFontLines()
获取范围('underline'
、'line-through'
或 'none'
)中单元格的线条样式。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); var results = range.getFontLines(); for (var i in results) { for (var j in results[i]) { Logger.log(results[i][j]); } }
返程
String[][]
- 与范围中的单元格相关联的字体线的二维数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFontSize()
返回单元格左上角相应范围的单元格内字体大小(以点为单位)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); Logger.log(range.getFontSize());
返程
Integer
- 字体大小(以点为单位)。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFontSizes()
返回范围内单元格的字体大小。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); var results = range.getFontSizes(); for (var i in results) { for (var j in results[i]) { Logger.log(results[i][j]); } }
返程
Integer[][]
- 一个二维数组,包含与范围内的单元格相关联的文本的字体大小。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFontStyle()
返回范围左上角的单元格的字体样式('italic'
或 'normal'
)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); Logger.log(range.getFontStyle());
返程
String
- 单元格文本的字体样式。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFontStyles()
返回范围内单元格的字体样式。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); var results = range.getFontStyles(); for (var i in results) { for (var j in results[i]) { Logger.log(results[i][j]); } }
返程
String[][]
- 一个二维数组,其中包含与范围中的单元格相关联的文本样式。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFontWeight()
返回单元格左上角的字体粗细(正常/粗体)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); Logger.log(range.getFontWeight());
返程
String
- 单元格文本的字体粗细。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFontWeights()
返回范围内单元格的字体粗细。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); var results = range.getFontWeights(); for (var i in results) { for (var j in results[i]) { Logger.log(results[i][j]); } }
返程
String[][]
- 与范围中的单元格相关联的文本字体粗细的二维数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFormula()
返回范围左上角的单元格的公式(A1 表示法),如果单元格为空或不含公式,则返回空字符串。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This assumes you have a function in B5 that sums up // B2:B4 var range = sheet.getRange("B5"); // Logs the calculated value and the formula Logger.log("Calculated value: %s Formula: %s", range.getValue(), range.getFormula());
返程
String
- 单元格的公式。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFormulaR1C1()
返回给定单元格的公式(R1C1 表示法),如果没有,则返回 null
。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B5"); var formula = range.getFormulaR1C1(); Logger.log(formula);
返程
String
- 采用 R1C1 表示法的公式。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFormulas()
返回范围中单元格的公式(采用 A1 表示法)。对于没有公式的单元格,2D 数组中的条目是空字符串。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B5:C6"); var formulas = range.getFormulas(); for (var i in formulas) { for (var j in formulas[i]) { Logger.log(formulas[i][j]); } }
返程
String[][]
- 字符串格式的二维公式数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getFormulasR1C1()
返回范围中单元格的公式(R1C1 表示法)。对于没有公式的单元格,2D 数组中的条目为 null
。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B5:C6"); var formulas = range.getFormulasR1C1(); for (var i in formulas) { for (var j in formulas[i]) { Logger.log(formulas[i][j]); } }
返程
String[][]
- 采用 R1C1 表示法的二维公式数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getGridId()
返回范围父工作表的网格 ID。ID 是随机的非负 int 值。
// Log the grid ID of the first sheet (by tab position) in the spreadsheet. var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); Logger.log(range.getGridId());
返程
Integer
- 父工作表的网格 ID。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getHeight()
返回范围的高度。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); // logs 3.0 Logger.log(range.getHeight());
返程
Integer
- 范围的高度。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getHorizontalAlignment()
返回单元格文本(左侧/中心/右侧)在范围左上角的水平对齐情况。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); Logger.log(range.getHorizontalAlignment());
返程
String
- 单元格中文本的水平对齐方式。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getHorizontalAlignments()
返回范围中单元格的水平对齐方式。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); var results = range.getHorizontalAlignments(); for (var i in results) { for (var j in results[i]) { Logger.log(results[i][j]); } }
返程
String[][]
- 与范围中的单元格相关联的文本的二维水平对齐数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getLastColumn()
返回结束列位置。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); // Logs "4.0" Logger.log(range.getLastColumn());
返程
Integer
- 范围在电子表格中的结束列位置。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getLastRow()
返回结束行位置。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); // Logs "4.0" Logger.log(range.getLastRow());
返程
Integer
- 在电子表格中,范围的结束日期行位置。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getMergedRanges()
返回 Range
对象数组,表示完全位于当前范围内或至少包含当前范围内的一个单元格的合并单元格。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("A1:B3"); var mergedRanges = range.getMergedRanges(); for (var i = 0; i < mergedRanges.length; i++) { Logger.log(mergedRanges[i].getA1Notation()); Logger.log(mergedRanges[i].getDisplayValue()); }
返程
getNextDataCell(direction)
从范围的第一列和所在行中的单元格处开始,返回给定方向(即包含数据的连续范围的单元格的边缘)或下一个边缘中位于单元格边缘的单元格中的下一个单元格。相当于在编辑器中输入
Ctrl+[arrow key]
。
// Assume the active spreadsheet is blank. var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("C3:E5"); // Logs "C1" Logger.log(range.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation());
参数
名称 | 类型 | 说明 |
---|---|---|
direction | Direction | 查找下一个数据区域边缘单元格的方向。 |
返程
Range
- 数据区域边缘单元格或电子表格边缘的单元格。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getNote()
返回与指定范围相关联的备注。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); Logger.log(range.getNote());
返程
String
- 与指定单元格关联的记事。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getNotes()
返回与范围中的单元格关联的备注。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); var results = range.getNotes(); for (var i in results) { for (var j in results[i]) { Logger.log(results[i][j]); } }
返程
String[][]
- 与范围中的单元格相关联的笔记的二维数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getNumColumns()
返回此范围内的列数。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D5"); Logger.log(range.getNumColumns());
返程
Integer
- 此范围中的列数。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getNumRows()
返回此范围内的行数。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D5"); Logger.log(range.getNumRows());
返程
Integer
- 此范围中的行数。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getNumberFormat()
获取指定范围左上角的单元格的数字或日期格式。Sheets API 文档中介绍了返回的格式模式。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("C4"); Logger.log(cell.getNumberFormat());
返程
String
- 范围左上角的单元格的数字格式。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getNumberFormats()
返回范围内单元格的数字格式或日期格式。Sheets API 文档中介绍了返回的格式模式。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B5:C6"); var formats = range.getNumberFormats(); for (var i in formats) { for (var j in formats[i]) { Logger.log(formats[i][j]); } }
返程
String[][]
- 一个二维数字格式数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getRichTextValue()
返回范围左上角的单元格的富文本值,如果单元格值不是文本,则返回 null
。
// Gets the Rich Text value of cell D4. var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("D4:F6"); var richText = range.getRichTextValue(); console.log(richText.getText());
返程
RichTextValue
- 范围左上角的单元格的富文本值;如果单元格值不是文本,则返回 null
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getRichTextValues()
返回范围中单元格的富文本值。
// Gets the Rich Text values for all cells in range B5:C6 var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("B5:C6"); var values = range.getRichTextValues(); for (var i = 0; i < values.length; i++) { for (var j = 0; j < values[i].length; j++) { console.log(values[i][j].getText()); } }
返程
RichTextValue[][]
- 一个二维富文本值数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getRow()
返回此范围的行位置。与 getRowIndex() 相同。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2"); Logger.log(range.getRow());
返程
Integer
- 范围的行数。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getRowIndex()
返回此范围的行位置。与 getRow() 相同。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2"); Logger.log(range.getRowIndex());
返程
Integer
- 范围的行数。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
另请参阅
getSheet()
返回此范围所属的工作表。
// 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 Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets the range A1:D10 on Sheet1. const range = sheet.getRange('A1:D10'); // Gets the sheet that the range belongs to. const rangeSheet = range.getSheet(); // Gets the sheet name and logs it to the console. console.log(rangeSheet.getName());
返程
Sheet
- 此范围所属的工作表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getTextDirection()
返回范围左上角的单元格的文本方向。如果通过自动检测确定单元格文本方向,则返回 null
。
// Get the text direction of cell B1. var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("B1:D4"); Logger.log(range.getTextDirection());
返程
TextDirection
- 范围左上角的单元格的文本方向。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getTextDirections()
返回范围中单元格的文本方向。对于使用自动检测功能的单元格,2D 数组中的条目为 null
。
// Get the text directions for all cells in range B5:C6 var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("B5:C6"); var directions = range.getTextDirections(); for (var i = 0; i < directions.length; i++) { for (var j = 0; j < directions[i].length; j++) { Logger.log(directions[i][j]); } }
返程
TextDirection[][]
- 一个二维文本路线数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getTextRotation()
返回范围左上角单元格的文本旋转设置。
// Log the text rotation settings for a cell. var sheet = SpreadsheetApp.getActiveSheet(); var cell = sheet.getRange("A1"); Logger.log(cell.getTextRotation());
返程
TextRotation
- 文字旋转设置。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getTextRotations()
返回范围中单元格的文本旋转设置。
var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("B2:D4"); var results = range.getTextRotations(); for (var i in results) { for (var j in results[i]) { var rotation = results[i][j]; Logger.log("Cell [%s, %s] has text rotation: %v", i, j, rotation); } }
返程
TextRotation[][]
- 与范围中的单元格相关联的二维文本旋转数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getTextStyle()
返回范围左上角的单元格的文本样式。
// Get the text style of cell D4. var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("D4:F6"); var style = range.getTextStyle(); Logger.log(style);
返程
TextStyle
- 范围左上角的单元格的文本样式。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getTextStyles()
返回范围中单元格的文本样式。
// Get the text styles for all cells in range B5:C6 var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("B5:C6"); var styles = range.getTextStyles(); for (var i = 0; i < styles.length; i++) { for (var j = 0; j < styles[i].length; j++) { Logger.log(styles[i][j]); } }
返程
TextStyle[][]
- 一个二维文本样式数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getValue()
返回范围中左上角单元格的值。该值可能是 Number
、Boolean
、Date
或 String
类型,具体取决于单元格的值。空单元格会返回一个空字符串。
// 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 Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets the range A1:D10 on Sheet1. const range = sheet.getRange('A1:D10'); // Gets the value of the top-left cell in the range and logs it to the console. console.log(range.getValue());
返程
Object
- 此单元格中的值。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getValues()
返回此范围值的矩形网格。
返回值的二维数组,按行编入索引,然后按列。这些值可以是 Number
、Boolean
、Date
或 String
类型,具体取决于单元格的值。空单元格由数组中的空字符串表示。请注意,虽然范围索引从 1, 1
开始,但 JavaScript 数组是从 [0][0]
编入索引的。
// The code below gets the values for the range C2:G8 // in the active spreadsheet. Note that this is a JavaScript array. var values = SpreadsheetApp.getActiveSheet().getRange(2, 3, 6, 4).getValues(); Logger.log(values[0][0]);在 Web 应用中,
Date
值不是合法参数。如果范围包含值为 Date
的单元格,则 getValues()
无法向 Web 应用返回数据。而是将从工作表中检索到的所有值转换为受支持的 JavaScript 原语(例如 Number
、Boolean
或 String
)。返程
Object[][]
- 一个二维值数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getVerticalAlignment()
返回单元格在范围左上角的垂直对齐(顶部/中间/底部)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); Logger.log(range.getVerticalAlignment());
返程
String
- 单元格中文本的垂直对齐方式。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getVerticalAlignments()
返回范围内单元格的垂直对齐方式。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); var results = range.getVerticalAlignments(); for (var i in results) { for (var j in results[i]) { Logger.log(results[i][j]); } }
返程
String[][]
- 与范围中的单元格相关联的文本垂直对齐的二维数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getWidth()
以列的形式返回范围的宽度。
// 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 Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets the range A1:D10 on Sheet1. const range = sheet.getRange('A1:D10'); // Gets the width of the range in number of columns and logs it to the console. console.log(range.getWidth());
返程
Integer
- 范围内的列数。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getWrap()
返回单元格中的文本是否换行。如需实现更精细的封装策略,请使用 getWrapStrategy()
。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); Logger.log(range.getWrap());
返程
Boolean
- 此单元格中的文本是否换行。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getWrapStrategies()
返回范围中单元格的文本换行策略。
// Get the text wrapping strategies for all cells in range B5:C6 var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("B5:C6"); var strategies = range.getWrapStrategies(); for (var i = 0; i < strategies.length; i++) { for (var j = 0; j < strategies[i].length; j++) { Logger.log(strategies[i][j]); } }
返程
WrapStrategy[][]
- 一个二维文本换行策略数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getWrapStrategy()
返回范围左上角的单元格的文本换行策略。
// Get the text wrapping strategy of cell B1. var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("B1:D4"); Logger.log(range.getWrapStrategy());
返程
WrapStrategy
- 表示左上角左上角单元格的文本换行策略。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getWraps()
返回单元格中的文本是否换行。如需实现更精细的封装策略,请使用 getWrapStrategies()
。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); var results = range.getVerticalAlignments(); for (var i in results) { for (var j in results[i]) { var isWrapped = results[i][j]; if (isWrapped) { Logger.log("Cell [%s, %s] has wrapped text", i, j); } } }
返程
Boolean[][]
- 与范围中的单元格相关联的文本垂直对齐的二维数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertCells(shiftDimension)
在此范围内插入空白单元格。新单元格会保留之前占用此范围的单元格中的所有格式。工作表中沿给定维度的现有数据会从插入的范围移出。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("A1:D10"); range.insertCells(SpreadsheetApp.Dimension.COLUMNS);
参数
名称 | 类型 | 说明 |
---|---|---|
shiftDimension | Dimension | 按现有维度偏移现有数据。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertCheckboxes()
向范围内的每个单元格插入复选框,配置时带有 true
表示勾选,false
表示未选中。将范围内所有单元格的值设置为 false
。
var range = SpreadsheetApp.getActive().getRange('A1:B10'); // Inserts checkboxes into each cell in the range A1:B10 configured with 'true' for checked // and 'false' for unchecked. Also, sets the value of each cell in the range A1:B10 to 'false'. range.insertCheckboxes();
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertCheckboxes(checkedValue)
向范围内的每个单元格插入复选框,并配置一个自定义值表示选中,空字符串表示未选中。将范围内每个单元格的值设置为空字符串。
var range = SpreadsheetApp.getActive().getRange('A1:B10'); // Inserts checkboxes into each cell in the range A1:B10 configured with 'yes' for checked // and the empty string for unchecked. Also, sets the value of each cell in the range A1:B10 to // the empty string. range.insertCheckboxes('yes');
参数
名称 | 类型 | 说明 |
---|---|---|
checkedValue | Object | 复选框数据验证的选中值。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insertCheckboxes(checkedValue, uncheckedValue)
向范围的每个单元格插入复选框,并配置有已选中和未选中状态的自定义值。将范围中每个单元格的值设置为自定义的未检查值。
var range = SpreadsheetApp.getActive().getRange('A1:B10'); // Inserts checkboxes into each cell in the range A1:B10 configured with 'yes' for checked // and 'no' for unchecked. Also, sets the value of each cell in the range A1:B10 to 'no'. range.insertCheckboxes('yes', 'no');
参数
名称 | 类型 | 说明 |
---|---|---|
checkedValue | Object | 复选框数据验证的选中值。 |
uncheckedValue | Object | 复选框数据验证的取消选中值。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
isBlank()
如果范围完全为空,则返回 true
。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D4"); Logger.log(range.isBlank());
返程
Boolean
- 如果范围为空,则为 true
;否则为 false
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
isChecked()
返回范围内所有单元格的复选框状态是否为“已勾选”。如果某些单元格已选中,其余单元格未选中,或者某些单元格没有复选框数据验证,则返回 null
。
var range = SpreadsheetApp.getActive().getRange('A1:A3'); // Inserts checkboxes and sets each cell value to 'no' in the range A1:A3. range.insertCheckboxes('yes', 'no'); var range1 = SpreadsheetApp.getActive().getRange('A1'); range1.setValue('yes'); // Sets the value of isRange1Checked as true as it contains the checked value. var isRange1Checked = range1.isChecked(); var range2 = SpreadsheetApp.getActive().getRange('A2'); range2.setValue('no'); // Sets the value of isRange2Checked as false as it contains the unchecked value. var isRange2Checked = range2.isChecked(); var range3 = SpreadsheetApp.getActive().getRange('A3'); range3.setValue('random'); // Sets the value of isRange3Checked as null, as it contains an invalid checkbox value. var isRange3Checked = range3.isChecked();
返程
Boolean
- 如果范围内的所有单元格都已选中,则范围内的所有单元格均为未选中状态,否则为 null
(如果任何单元格未选中或没有复选框数据验证)。true
false
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
isEndColumnBounded()
确定范围的末尾是否绑定到特定列。例如,对于范围 A1:B10
或 B:B
的范围,这些范围绑定到范围末尾处的列;此方法返回 true
;对于范围 3:7
或 A1:5
范围,范围仅绑定到范围末尾的特定行,此方法会返回 false
。
// 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 Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets the range A1:D10 on Sheet1. const range = sheet.getRange('A1:D10'); // Determines if the end of the range is bound to a particular column and logs it to the // console. console.log(range.isEndColumnBounded());
返程
Boolean
- 如果范围的末尾绑定到特定列,则为 true
;否则为 false
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
isEndRowBounded()
确定范围的末尾是否绑定到特定行。例如,对于范围 A1:B10
或 3:7
范围中的行,此方法会返回 true
;对于范围 B:B
或 A1:C
,范围仅绑定到范围末尾的特定列,此方法会返回 false
。
// 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 Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets the range A1:D10 on Sheet1. const range = sheet.getRange('A1:D10'); // Determines if the end of the range is bound to a particular row and logs it to the console. console.log(range.isEndRowBounded());
返程
Boolean
- 如果范围的末尾绑定到特定行,则为 true
;否则为 false
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
isPartOfMerge()
如果当前范围的单元格与任何合并的单元格重叠,则返回 true
。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("A1:B3"); // True if any of the cells in A1:B3 is included in a merge. var isPartOfMerge = range.isPartOfMerge();
返程
Boolean
- 如果范围与任何合并的单元格重叠,则为 true
,否则返回 false
。
isStartColumnBounded()
确定此范围的开头是否绑定到特定列。例如,对于范围 A1:B10
或 B:B
的范围,这些范围绑定到范围开头的列,此方法返回 true
;对于范围 3:7
,范围仅绑定到范围开始时的行,此方法会返回 false
。
// 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 Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets the range A1:D10 on Sheet1. const range = sheet.getRange('A1:D10'); // Determines if the start of the range is bound to a particular column and logs it to the // console. console.log(range.isStartColumnBounded());
返程
Boolean
- 如果范围的开头绑定到特定列,则为 true
;否则为 false
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
isStartRowBounded()
确定范围的开头是否绑定到特定行。例如,对于范围 A1:B10
或 3:7
,范围绑定到范围开始时的行,此方法会返回 true
;对于范围 B:B
,范围仅绑定到范围开始时的特定列,此方法会返回 false
。
// 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 Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets the range A1:D10 on Sheet1. const range = sheet.getRange('A1:D10'); // Determines if the start of the range is bound to a particular row and logs it to the // console. console.log(range.isStartRowBounded());
返程
Boolean
- 如果范围的开头绑定到特定行,则为 true
;否则为 false
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
merge()
将范围中的单元格合并为一个块。
var sheet = SpreadsheetApp.getActiveSheet(); // The code below 2-dimensionally merges the cells in A1 to B3 sheet.getRange('A1:B3').merge();
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
mergeAcross()
合并范围内的单元格。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // The code below merges cells C5:E5 into one cell var range1 = sheet.getRange("C5:E5"); range1.mergeAcross(); // The code below creates 2 horizontal cells, F5:H5 and F6:H6 var range2 = sheet.getRange("F5:H6"); range2.mergeAcross();
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
mergeVertically()
将范围中的单元格合并在一起。
var sheet = SpreadsheetApp.getActiveSheet(); // The code below vertically merges the cells in A1 to A10 sheet.getRange('A1:A10').mergeVertically(); // The code below creates 3 merged columns: B1 to B10, C1 to C10, and D1 to D10 sheet.getRange('B1:D10').mergeVertically();
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
moveTo(target)
从此范围剪切并粘贴(格式和值)至目标范围。
// The code below moves the first 5 columns over to the 6th column var sheet = SpreadsheetApp.getActiveSheet() sheet.getRange("A1:E").moveTo(sheet.getRange("F1"));
参数
名称 | 类型 | 说明 |
---|---|---|
target | Range | 将此范围复制到的目标范围;仅左上角单元格位置相关。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
offset(rowOffset, columnOffset)
返回根据给定行数和列数(可以为负值)从此范围偏移的新范围。新范围的大小与原始范围相同。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("A1"); // newCell references B2 var newCell = cell.offset(1, 1);
参数
名称 | 类型 | 说明 |
---|---|---|
rowOffset | Integer | 从范围的左上角单元格向下流动的行数;负值表示从范围左上角单元格向上指向的行。 |
columnOffset | Integer | 范围左上角的单元格的列数;负数值表示范围左上角的单元格左侧的列。 |
返程
Range
- 此范围用于串联。
offset(rowOffset, columnOffset, numRows)
返回相对于当前范围的新范围,该范围中的左上角会按给定的行和列与当前范围偏移,并采用单元格中的指定高度。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("A1"); // newCell references B2:B3 var newRange = cell.offset(1, 1, 2);
参数
名称 | 类型 | 说明 |
---|---|---|
rowOffset | Integer | 从范围的左上角单元格向下流动的行数;负值表示从范围左上角单元格向上指向的行。 |
columnOffset | Integer | 范围左上角的单元格的列数;负数值表示范围左上角的单元格左侧的列。 |
numRows | Integer | 新范围行的高度。 |
返程
Range
- 此范围用于串联。
offset(rowOffset, columnOffset, numRows, numColumns)
返回相对于当前范围的新范围,其左上角可通过指定行和列与当前范围进行偏移,并且具有指定的高度和宽度。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("A1"); // newCell references B2:C3 var newRange = cell.offset(1, 1, 2, 2);
参数
名称 | 类型 | 说明 |
---|---|---|
rowOffset | Integer | 从范围的左上角单元格向下流动的行数;负值表示从范围左上角单元格向上指向的行。 |
columnOffset | Integer | 范围左上角的单元格的列数;负数值表示范围左上角的单元格左侧的列。 |
numRows | Integer | 新范围行的高度。 |
numColumns | Integer | 新范围的列宽。 |
返程
Range
- 此范围用于串联。
protect()
创建一个可以防止范围被修改的对象(具有相应权限的用户除外)。除非脚本实际更改范围的编辑器列表(通过调用 Protection.removeEditor(emailAddress)
、Protection.removeEditor(user)
、Protection.removeEditors(emailAddresses)
、Protection.addEditor(emailAddress)
、Protection.addEditor(user)
、Protection.addEditors(emailAddresses)
或为 Protection.setDomainEdit(editable)
设置新值),否则这些权限反映的是电子表格本身,这意味着该范围实际上不会受到保护。如果相应范围已受到保护,则此方法会创建一个与现有范围重叠的新受保护范围。如果某个单元格受到多个受保护范围的保护,且其中任何一个范围都阻止特定用户编辑该单元格,则不允许该用户修改此单元格。
// Protect range A1:B10, then remove all other users from the list of editors. var ss = SpreadsheetApp.getActive(); var range = ss.getRange('A1:B10'); var protection = range.protect().setDescription('Sample protected range'); // Ensure the current user is an editor before removing others. Otherwise, if the user's edit // permission comes from a group, the script throws an exception upon removing the group. var me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); }
返程
Protection
- 表示保护设置的对象。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
randomize()
随机排列指定范围中各行的顺序。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("A1:C7"); // Randomizes the range range.randomize();
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeCheckboxes()
从范围中移除所有复选框。清除每个单元格的数据验证。此外,如果单元格包含选中或未选中的值,则清除其值。
var range = SpreadsheetApp.getActive().getRange('A1:B10'); // Inserts checkboxes and sets each cell value to 'no' in the range A1:B10. range.insertCheckboxes('yes', 'no'); var range1 = SpreadsheetApp.getActive().getRange('A1'); range1.setValue('yes'); // Removes the checkbox data validation in cell A1 and clears its value. range1.removeCheckboxes(); var range2 = SpreadsheetApp.getActive().getRange('A2'); range2.setValue('random'); // Removes the checkbox data validation in cell A2 but does not clear its value. range2.removeCheckboxes();
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeDuplicates()
移除此范围内的行,这些行包含的值与之前任何行中的值重复。系统会将具有相同值但字母大小写、格式设置或公式不同的行视为重复行。此方法还会移除视图中隐藏的重复行(例如,由于过滤条件)。系统不会移除超出此范围的内容。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B1:D7"); // Remove duplicate rows in the range. range.removeDuplicates();
返程
Range
- 移除重复内容后生成的范围。每移除一行,该范围就会缩小一行。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeDuplicates(columnsToCompare)
移除此范围内包含指定列中与之前任何行重复的值的行。系统会将具有相同值但字母大小写、格式设置或公式不同的行视为重复行。此方法还会移除视图中隐藏的重复行(例如,由于过滤条件)。系统不会移除超出此范围的内容。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B1:D7"); // Remove rows which have duplicate values in column B. range.removeDuplicates([2]); // Remove rows which have duplicate values in both columns B and D. range.removeDuplicates([2,4]);
参数
名称 | 类型 | 说明 |
---|---|---|
columnsToCompare | Integer[] | 要分析是否存在重复值的列。如果未提供任何列,则系统会分析所有列以寻找重复列。 |
返程
Range
- 移除重复内容后生成的范围。每移除一行,该范围就会缩小一行。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setBackground(color)
以 CSS 表示法(例如 '#ffffff'
或 'white'
)设置范围内所有单元格的背景颜色。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("B2:D5"); range.setBackground("red");
参数
名称 | 类型 | 说明 |
---|---|---|
color | String | 以 CSS 表示法表示的颜色代码(例如 '#ffffff' 或 'white' );null 值会重置颜色。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setBackgroundObject(color)
设置范围内所有单元格的背景颜色。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var bgColor = SpreadsheetApp.newColor() .setThemeColor(SpreadsheetApp.ThemeColorType.BACKGROUND) .build(); var range = sheet.getRange("B2:D5"); range.setBackgroundObject(bgColor);
参数
名称 | 类型 | 说明 |
---|---|---|
color | Color | 要设置的背景颜色;null 值会重置背景颜色。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setBackgroundObjects(color)
设置矩形背景颜色的网格(必须与此范围的尺寸匹配)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var colorAccent1 = SpreadsheetApp.newColor() .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT1) .build(); var colorAccent2 = SpreadsheetApp.newColor() .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT2) .build(); var colorAccent3 = SpreadsheetApp.newColor() .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT3) .build(); var colorAccent4 = SpreadsheetApp.newColor() .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT4) .build(); var colors = [ [colorAccent1, colorAccent2], [colorAccent3, colorAccent4] ]; var cell = sheet.getRange("B5:C6"); cell.setBackgroundObjects(colors);
参数
名称 | 类型 | 说明 |
---|---|---|
color | Color[][] | 一个二维颜色数组;null 值用于重置颜色。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setBackgroundRGB(red, green, blue)
使用 RGB 值(0 到 255 [整数] 之间的整数)将背景设置为指定颜色。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); // Sets the background to white cell.setBackgroundRGB(255, 255, 255); // Sets the background to red cell.setBackgroundRGB(255, 0, 0);
参数
名称 | 类型 | 说明 |
---|---|---|
red | Integer | 红色值(采用 RGB 表示法)。 |
green | Integer | 绿色值(采用 RGB 表示法)。 |
blue | Integer | 以 RGB 表示法表示的蓝色值。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setBackgrounds(color)
设置背景颜色的矩形网格(必须与此范围的尺寸匹配)。颜色采用 CSS 表示法(例如 '#ffffff'
或 'white'
)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var colors = [ ["red", "white", "blue"], ["#FF0000", "#FFFFFF", "#0000FF"] // These are the hex equivalents ]; var cell = sheet.getRange("B5:D6"); cell.setBackgrounds(colors);
参数
名称 | 类型 | 说明 |
---|---|---|
color | String[][] | 采用 CSS 表示法(例如 '#ffffff' 或 'white' )的二维颜色数组;null 值用于重置颜色。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setBorder(top, left, bottom, right, vertical, horizontal)
设置边框属性。有效值为 true
(开启)、false
(关闭)和 null
(无变化)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); // Sets borders on the top and bottom, but leaves the left and right unchanged cell.setBorder(true, null, true, null, false, false);
参数
名称 | 类型 | 说明 |
---|---|---|
top | Boolean | true 表示边框,false 表示无边框,null 表示无变化。 |
left | Boolean | true 表示边框,false 表示无边框,null 表示无变化。 |
bottom | Boolean | true 表示边框,false 表示无边框,null 表示无变化。 |
right | Boolean | true 表示边框,false 表示无边框,null 表示无变化。 |
vertical | Boolean | true 表示内部垂直边框,false 表示无边框,null 表示保持不变。 |
horizontal | Boolean | true 表示内部水平边框,false 表示无边框,null 表示保持不变。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setBorder(top, left, bottom, right, vertical, horizontal, color, style)
设置颜色和/或样式的边框属性。有效值为 true
(开启)、false
(关闭)和 null
(无变化)。如需设置颜色,请使用 CSS 表示法中的颜色(例如 '#ffffff'
或 'white'
)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); // Sets borders on the top and bottom, but leaves the left and right unchanged // Also sets the color to "red", and the border to "DASHED". cell.setBorder(true, null, true, null, false, false, "red", SpreadsheetApp.BorderStyle.DASHED);
参数
名称 | 类型 | 说明 |
---|---|---|
top | Boolean | true 表示边框,false 表示无边框,null 表示无变化。 |
left | Boolean | true 表示边框,false 表示无边框,null 表示无变化。 |
bottom | Boolean | true 表示边框,false 表示无边框,null 表示无变化。 |
right | Boolean | true 表示边框,false 表示无边框,null 表示无变化。 |
vertical | Boolean | true 表示内部垂直边框,false 表示无边框,null 表示保持不变。 |
horizontal | Boolean | true 表示内部水平边框,false 表示无边框,null 表示保持不变。 |
color | String | 以 CSS 表示法表示的颜色(例如 '#ffffff' 或 'white' ),null 表示默认颜色(黑色)。 |
style | BorderStyle | 边框的样式,null 表示默认样式(纯色)。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setDataValidation(rule)
为范围中的所有单元格设置一条数据验证规则。
// Set the data validation rule for cell A1 to require a value from B1:B10. var cell = SpreadsheetApp.getActive().getRange('A1'); var range = SpreadsheetApp.getActive().getRange('B1:B10'); var rule = SpreadsheetApp.newDataValidation().requireValueInRange(range).build(); cell.setDataValidation(rule);
参数
名称 | 类型 | 说明 |
---|---|---|
rule | DataValidation | 要设置的数据验证规则,或用于移除数据验证的 null 。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setDataValidations(rules)
为范围中的所有单元格设置数据验证规则。此方法采用一个二维数据验证数组,先后按行编入索引。数组维度必须与范围维度相对应。
// Set the data validation rules for Sheet1!A1:B5 to require a value from Sheet2!A1:A10. var destinationRange = SpreadsheetApp.getActive().getSheetByName('Sheet1').getRange('A1:B5'); var sourceRange = SpreadsheetApp.getActive().getSheetByName('Sheet2').getRange('A1:A10'); var rule = SpreadsheetApp.newDataValidation().requireValueInRange(sourceRange).build(); var rules = destinationRange.getDataValidations(); for (var i = 0; i < rules.length; i++) { for (var j = 0; j < rules[i].length; j++) { rules[i][j] = rule; } } destinationRange.setDataValidations(rules);
参数
名称 | 类型 | 说明 |
---|---|---|
rules | DataValidation[][] | 要设置的二维数据验证规则;null 值用于移除数据验证。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFontColor(color)
以 CSS 表示法(如 '#ffffff'
或 'white'
)设置字体颜色。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); cell.setFontColor("red");
参数
名称 | 类型 | 说明 |
---|---|---|
color | String | 以 CSS 表示法表示的字体颜色(例如 '#ffffff' 或 'white' );null 值会重置颜色。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFontColorObject(color)
设置指定范围的字体颜色。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var color = SpreadsheetApp.newColor() .setThemeColor(SpreadsheetApp.ThemeColorType.TEXT) .build(); var cell = sheet.getRange("B2"); cell.setFontColor(color);
参数
名称 | 类型 | 说明 |
---|---|---|
color | Color | 要设置的字体颜色;null 值可重置颜色。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFontColorObjects(colors)
设置矩形颜色网格(必须与此范围的尺寸相匹配)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var colorAccent1 = SpreadsheetApp.newColor() .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT1) .build(); var colorAccent2 = SpreadsheetApp.newColor() .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT2) .build(); var colorAccent3 = SpreadsheetApp.newColor() .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT3) .build(); var colorAccent4 = SpreadsheetApp.newColor() .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT4) .build(); var colors = [ [colorAccent1, colorAccent2], [colorAccent3, colorAccent4] ]; var cell = sheet.getRange("B5:C6"); cell.setFontColorObjects(colors);
参数
名称 | 类型 | 说明 |
---|---|---|
colors | Color[][] | 一个二维颜色数组;null 值用于重置字体颜色。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFontColors(colors)
设置矩形颜色网格(必须与此范围的尺寸相匹配)。颜色以 CSS 表示法(例如 '#ffffff'
或 'white'
)表示。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var colors = [ ["red", "white", "blue"], ["#FF0000", "#FFFFFF", "#0000FF"] // These are the hex equivalents ]; var cell = sheet.getRange("B5:D6"); cell.setFontColors(colors);
参数
名称 | 类型 | 说明 |
---|---|---|
colors | Object[][] | 采用 CSS 表示法(例如 '#ffffff' 或 'white' )的二维颜色数组;null 值用于重置颜色。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFontFamilies(fontFamilies)
设置一个矩形字体系列网格(必须与此范围的尺寸相匹配)。字体系列的示例包括“SendGrid”或“Helvetica”。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var fonts = [ ["Arial", "Helvetica", "Verdana"], ["Courier New", "Arial", "Helvetica] ]; var cell = sheet.getRange("B2:D3"); cell.setFontFamilies(fonts);
参数
名称 | 类型 | 说明 |
---|---|---|
fontFamilies | Object[][] | 一个二维字体系列数组;null 值会重置字体系列。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFontFamily(fontFamily)
设置字体系列,例如“SendGrid”或“Helvetica”。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); cell.setFontFamily("Helvetica");
参数
名称 | 类型 | 说明 |
---|---|---|
fontFamily | String | 要设置的字体系列;null 值会重置字体系列。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFontLine(fontLine)
设置给定范围('underline'
、'line-through'
或 'none'
)的字体行样式。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); cell.setFontLine("line-through");
参数
名称 | 类型 | 说明 |
---|---|---|
fontLine | String | 字体样式('underline' 、'line-through' 或 'none' );null 值可重置字体线条样式。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFontLines(fontLines)
设置矩形线条网格网格(必须与此范围的尺寸相匹配)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // The size of the two-dimensional array must match the size of the range. var fontLines = [ ["underline", "line-through", "none"] ]; var range = sheet.getRange("B2:D2"); range.setFontLines(fontLines);
参数
名称 | 类型 | 说明 |
---|---|---|
fontLines | Object[][] | 一个二维字体线样式('underline' 、'line-through' 或 'none' );null 值用于重置字体行样式。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFontSize(size)
设置字号,其中字号是要使用的点大小。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); cell.setFontSize(20);
参数
名称 | 类型 | 说明 |
---|---|---|
size | Integer | 字体大小(以点为单位)。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFontSizes(sizes)
设置一个字体大小网格(必须与此范围的尺寸相匹配)。以点为单位。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // The size of the two-dimensional array must match the size of the range. var fontSizes = [ [16, 20, 24] ]; var range = sheet.getRange("B2:D2"); range.setFontSizes(fontSizes);
参数
名称 | 类型 | 说明 |
---|---|---|
sizes | Object[][] | 一个二维尺寸数组, |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFontStyle(fontStyle)
为指定范围('italic'
或 'normal'
)设置字体样式。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); cell.setFontStyle("italic");
参数
名称 | 类型 | 说明 |
---|---|---|
fontStyle | String | 字体样式为 'italic' 或 'normal' ;null 值会重置字体样式。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFontStyles(fontStyles)
设置矩形字体网格(必须与此范围的尺寸相匹配)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // The size of the two-dimensional array must match the size of the range. var fontStyles = [ ["italic", "normal"] ]; var range = sheet.getRange("B2:C2"); range.setFontStyles(fontStyles);
参数
名称 | 类型 | 说明 |
---|---|---|
fontStyles | Object[][] | 一个二维字体样式数组,可以是 'italic' 或 'normal' ;null 值可重置字体样式。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFontWeight(fontWeight)
为指定范围(正常/粗体)设置字体粗细。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); cell.setFontWeight("bold");
参数
名称 | 类型 | 说明 |
---|---|---|
fontWeight | String | 字体粗细('bold' 或 'normal' );null 值可重置字体粗细。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFontWeights(fontWeights)
设置字体粗细的矩形网格(必须与此范围的尺寸相匹配)。例如,“粗体”就是一种字体粗细。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // The size of the two-dimensional array must match the size of the range. var fontStyles = [ [ "bold", "bold", "normal" ] ]; var range = sheet.getRange("B2:D2"); range.setFontWeights(fontStyles);
参数
名称 | 类型 | 说明 |
---|---|---|
fontWeights | Object[][] | 一个二维字体粗细数组('bold' 或 'normal' );null 值用于重置字体粗细。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFormula(formula)
更新此范围的公式。指定公式必须采用 A1 表示法。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B5"); cell.setFormula("=SUM(B3:B4)");
参数
名称 | 类型 | 说明 |
---|---|---|
formula | String | 一个字符串,表示要为单元格设置的公式。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFormulaR1C1(formula)
更新此范围的公式。指定的公式必须采用 R1C1 表示法。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B5"); // This sets the formula to be the sum of the 3 rows above B5 cell.setFormulaR1C1("=SUM(R[-3]C[0]:R[-1]C[0])");
参数
名称 | 类型 | 说明 |
---|---|---|
formula | String | 一个字符串公式。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFormulas(formulas)
设置矩形公式网格(必须与此范围的维度匹配)。指定的公式必须采用 A1 表示法。此方法采用二维公式数组,先按行排序,然后按列编入索引。数组维度必须与范围维度相对应。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This sets the formulas to be a row of sums, followed by a row of averages right below. // The size of the two-dimensional array must match the size of the range. var formulas = [ ["=SUM(B2:B4)", "=SUM(C2:C4)", "=SUM(D2:D4)"], ["=AVERAGE(B2:B4)", "=AVERAGE(C2:C4)", "=AVERAGE(D2:D4)"] ]; var cell = sheet.getRange("B5:D6"); cell.setFormulas(formulas);
参数
名称 | 类型 | 说明 |
---|---|---|
formulas | String[][] | 公式二维字符串数组。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setFormulasR1C1(formulas)
设置矩形公式网格(必须与此范围的维度匹配)。指定的公式必须采用 R1C1 表示法。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // This creates formulas for a row of sums, followed by a row of averages. var sumOfRowsAbove = "=SUM(R[-3]C[0]:R[-1]C[0])"; var averageOfRowsAbove = "=AVERAGE(R[-4]C[0]:R[-2]C[0])"; // The size of the two-dimensional array must match the size of the range. var formulas = [ [sumOfRowsAbove, sumOfRowsAbove, sumOfRowsAbove], [averageOfRowsAbove, averageOfRowsAbove, averageOfRowsAbove] ]; var cell = sheet.getRange("B5:D6"); // This sets the formula to be the sum of the 3 rows above B5. cell.setFormulasR1C1(formulas);
参数
名称 | 类型 | 说明 |
---|---|---|
formulas | String[][] | R1C1 格式的二维公式数组。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setHorizontalAlignment(alignment)
为给定范围(左/中/右)设置水平(左到右)对齐。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); cell.setHorizontalAlignment("center");
参数
名称 | 类型 | 说明 |
---|---|---|
alignment | String | 对齐方式:'left' 、'center' 或 'normal' ;null 值可重置对齐方式。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setHorizontalAlignments(alignments)
设置水平对齐的矩形网格。请参阅 setHorizontalAlignment(alignment)
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // The size of the two-dimensional array must match the size of the range. var horizontalAlignments = [ [ "left", "right", "center" ] ]; var range = sheet.getRange("B2:D2"); range.setHorizontalAlignments(horizontalAlignments);
参数
名称 | 类型 | 说明 |
---|---|---|
alignments | Object[][] | 一个二维对齐数组('left' 、'center' 或 'normal' );null 值可重置对齐。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
另请参阅
setNote(note)
将备注设为指定值。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); cell.setNote("This is a note");
参数
名称 | 类型 | 说明 |
---|---|---|
note | String | 要为范围设置的备注值;null 值可移除备注。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setNotes(notes)
设置用于笔记的矩形网格(必须与此范围的尺寸相匹配)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // The size of the two-dimensional array must match the size of the range. var notes = [ ["it goes", "like this", "the fourth, the fifth"], ["the minor fall", "and the", "major lift"] ]; var cell = sheet.getRange("B2:D3"); cell.setNotes(notes)
参数
名称 | 类型 | 说明 |
---|---|---|
notes | Object[][] | 一个二维备注数组;null 值用于移除备注。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
另请参阅
setNumberFormat(numberFormat)
将数字或日期格式设为指定的格式字符串。Sheets API 文档中介绍了可接受的格式模式。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); // Always show 3 decimal points cell.setNumberFormat("0.000");
参数
名称 | 类型 | 说明 |
---|---|---|
numberFormat | String | 数字格式的字符串。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setNumberFormats(numberFormats)
设置数字或日期格式的矩形网格(必须与此范围的尺寸相匹配)。这些值是格式表格字符串,如 Sheets API 文档中所述。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // The size of the two-dimensional array must match the size of the range. var formats = [ [ "0.000", "0,000,000", "$0.00" ] ]; var range = sheet.getRange("B2:D2"); range.setNumberFormats(formats);
参数
名称 | 类型 | 说明 |
---|---|---|
numberFormats | Object[][] | 二维数字格式数组。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setRichTextValue(value)
为范围中的单元格设置富文本值。
// Sets all cells in range B2:D4 to have the text "Hello world", with "Hello" bolded. var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("B2:D4"); var bold = SpreadsheetApp.newTextStyle() .setBold(true) .build(); var richText = SpreadsheetApp.newRichTextValue() .setText("Hello world") .setTextStyle(0, 5, bold) .build(); range.setRichTextValue(richText);
参数
名称 | 类型 | 说明 |
---|---|---|
value | RichTextValue | 所需的富文本值。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setRichTextValues(values)
用于设置由富文本值组成的矩形网格。
// Sets the cells in range A1:A2 to have Rich Text values. var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("A1:A2"); var bold = SpreadsheetApp.newTextStyle() .setBold(true) .build(); var italic = SpreadsheetApp.newTextStyle() .setItalic(true) .build(); var richTextA1 = SpreadsheetApp.newRichTextValue() .setText("This cell is bold") .setTextStyle(bold) .build(); var richTextA2 = SpreadsheetApp.newRichTextValue() .setText("bold words, italic words") .setTextStyle(0, 11, bold) .setTextStyle(12, 24, italic) .build(); range.setRichTextValues([[richTextA1], [richTextA2]]);
参数
名称 | 类型 | 说明 |
---|---|---|
values | RichTextValue[][] | 所需的富文本值。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setShowHyperlink(showHyperlink)
设置范围是否应显示超链接。
// Opens the spreadsheet file by its URL. If you created your script from within a // Google Sheets file, you can useSpreadsheetApp.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 cell A30 and sets its hyperlink value. const range = sheet.getRange('A30'); range.setValue('https://www.example.com'); // Sets cell A30 to show hyperlinks. range.setShowHyperlink(true);
参数
名称 | 类型 | 说明 |
---|---|---|
showHyperlink | Boolean | 是否显示超链接。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setTextDirection(direction)
为范围中的单元格设置文本方向。如果指定方向为 null
,则系统会推断该方向,然后将其设置。
// Sets right-to-left text direction for the range. var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("B5:C6"); range.setTextDirection(SpreadsheetApp.TextDirection.RIGHT_TO_LEFT);
参数
名称 | 类型 | 说明 |
---|---|---|
direction | TextDirection | 所需的文本方向;如果为 null ,则系统会在设置之前推断方向。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setTextDirections(directions)
设置一个矩形文本网格。如果指定方向为 null
,则系统会推断方向,然后进行设置。
// Copies all of the text directions from range A1:B2 over to range C5:D6. var sheet = SpreadsheetApp.getActiveSheet(); var range1 = sheet.getRange("A1:B2"); var range2 = sheet.getRange("C5:D6"); range2.setTextRotations(range1.getTextDirections());
参数
名称 | 类型 | 说明 |
---|---|---|
directions | TextDirection[][] | 所需的文本路线;如果指定方向为 null ,则系统会在设置之前推断该路线。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setTextRotation(degrees)
对范围中的单元格进行文本旋转设置。该输入对应于标准文本方向和所需方向之间的角度。输入零表示文本设置为标准方向。
对于从左到右的文本方向,正角度为逆时针方向,而对于从右到左方向,则方向为顺时针方向。
// Sets all cell's in range B2:D4 to have text rotated up 45 degrees. var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("B2:D4"); range.setTextRotation(45);
参数
名称 | 类型 | 说明 |
---|---|---|
degrees | Integer | 标准方向与所需方向之间的期望角度。对于从左到右的文本,逆时针方向为正角度。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setTextRotation(rotation)
为范围中的单元格设定文本旋转设置。
// Sets all cell's in range B2:D4 to have the same text rotation settings as cell A1. var sheet = SpreadsheetApp.getActiveSheet(); var rotation = sheet.getRange("A1").getTextRotation(); sheet.getRange("B2:D4").setTextRotation(rotation);
参数
名称 | 类型 | 说明 |
---|---|---|
rotation | TextRotation | 所需的文本旋转设置。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setTextRotations(rotations)
设置矩形文本旋转网格。
// Copies all of the text rotations from range A1:B2 over to range C5:D6. var sheet = SpreadsheetApp.getActiveSheet(); var range1 = sheet.getRange("A1:B2"); var range2 = sheet.getRange("C5:D6"); range2.setTextRotations(range1.getTextRotations());
参数
名称 | 类型 | 说明 |
---|---|---|
rotations | TextRotation[][] | 所需的文本旋转设置。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setTextStyle(style)
为范围中的单元格设置文本样式。
// Sets the cells in range C5:D6 to have underlined size 15 font. var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("C5:D6"); var style = SpreadsheetApp.newTextStyle() .setFontSize(15) .setUnderline(true) .build(); range.setTextStyle(style);
参数
名称 | 类型 | 说明 |
---|---|---|
style | TextStyle | 所需的文本样式。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setTextStyles(styles)
设置矩形文本网格。
// Sets text styles for cells in range A1:B2 var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("A1:B2"); var bold = SpreadsheetApp.newTextStyle() .setBold(true) .build(); var otherStyle = SpreadsheetApp.newTextStyle() .setBold(true) .setUnderline(true) .setItalic(true) .setForegroundColor("#335522") .setFontSize(44) .build(); range.setTextStyles([[bold, otherStyle], [otherStyle, bold]]);
参数
名称 | 类型 | 说明 |
---|---|---|
styles | TextStyle[][] | 所需的文本样式。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setValue(value)
设置范围的值。值可以是数字、字符串、布尔值或日期。如果以 '='
开头,则解译为公式。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); cell.setValue(100);
参数
名称 | 类型 | 说明 |
---|---|---|
value | Object | 范围的值。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setValues(values)
设置一个矩形值网格(必须与此范围的尺寸相匹配)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // The size of the two-dimensional array must match the size of the range. var values = [ [ "2.000", "1,000,000", "$2.99" ] ]; var range = sheet.getRange("B2:D2"); range.setValues(values);
参数
名称 | 类型 | 说明 |
---|---|---|
values | Object[][] | 一个二维值数组。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setVerticalAlignment(alignment)
为给定范围(顶部/中间/底部)设置垂直(从上到下)对齐。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); cell.setVerticalAlignment("middle");
参数
名称 | 类型 | 说明 |
---|---|---|
alignment | String | 对齐方式:'top' 、'middle' 或 'bottom' ;null 值可重置对齐方式。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setVerticalAlignments(alignments)
设置一个由垂直对齐的矩形网格(必须与此范围的尺寸匹配)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // The size of the two-dimensional array must match the size of the range. var alignments = [ [ "top", "middle", "bottom" ] ]; var range = sheet.getRange("B2:D2"); range.setVerticalAlignments(alignments);
参数
名称 | 类型 | 说明 |
---|---|---|
alignments | Object[][] | 一个二维对齐数组('top' 、'middle' 或 'bottom' );null 值可重置对齐。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
另请参阅
setVerticalText(isVertical)
设置是否堆叠范围内单元格的文本。如果文本是垂直堆叠的,则忽略文本旋转度设置。
// Sets all cell's in range B2:D4 to have vertically stacked text. var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("B2:D4"); range.setVerticalText(true);
参数
名称 | 类型 | 说明 |
---|---|---|
isVertical | Boolean | 是否堆叠文本。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setWrap(isWrapEnabled)
设置给定范围的单元格换行。
已启用自动换行(默认)的单元格会自动调整大小,以显示完整内容。如果单元格已停用自动换行功能,则会在单元格中尽可能多地显示内容,而不会调整其大小或让其运行多行。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B2"); cell.setWrap(true);
参数
名称 | 类型 | 说明 |
---|---|---|
isWrapEnabled | Boolean | 是否换行。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setWrapStrategies(strategies)
设置一个矩形封装网格。
// Copies all of the wrap strategies from range A1:B2 over to range C5:D6. var sheet = SpreadsheetApp.getActiveSheet(); var range1 = sheet.getRange("A1:B2"); var range2 = sheet.getRange("C5:D6"); range2.setWrapStrategies(range1.getWrapStrategies());
参数
名称 | 类型 | 说明 |
---|---|---|
strategies | WrapStrategy[][] | 所需的封装策略。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setWrapStrategy(strategy)
为范围中的单元格设置文本换行策略。
// Sets all cells in range B2:D4 to use the clip wrap strategy. var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("B2:D4"); range.setWrapStrategy(SpreadsheetApp.WrapStrategy.CLIP);
参数
名称 | 类型 | 说明 |
---|---|---|
strategy | WrapStrategy | 所需的封装策略。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setWraps(isWrapEnabled)
设置矩形自动换行政策网格(必须与此范围的尺寸相匹配)。已启用自动换行(默认)的单元格可调整大小以显示其完整内容。如果单元格已停用自动换行功能,则会在单元格中尽可能多地显示内容,而不会调整其大小或让其运行多行。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // The size of the two-dimensional array must match the size of the range. var wraps = [ [ true, true, false ] ]; var range = sheet.getRange("B2:D2"); range.setWraps(wraps);
参数
名称 | 类型 | 说明 |
---|---|---|
isWrapEnabled | Object[][] | 二维数组的换行变量,用于确定是否在单元格中封装文本。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
另请参阅
shiftColumnGroupDepth(delta)
按指定数量更改范围的列分组深度。
这样可以创建、修改或删除与范围相交的群组。对于正增量,系统会创建和/或修改组;对于负增量,则会销毁和/或修改组。
将组深度降低到零或大于 8 时不起作用。
如果 column group control position
为 BEFORE
,则在尝试偏移第一行的深度时抛出错误。
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; var range = sheet.getActiveRange(); // The column grouping depth is increased by 1. range.shiftColumnGroupDepth(1); // The column grouping depth is decreased by 1. range.shiftColumnGroupDepth(-1);
参数
名称 | 类型 | 说明 |
---|---|---|
delta | Integer | 用于更改此范围列组深度的金额。 |
返程
Range
- 此范围用于串联。
抛出
Error
- 尝试在控件位置为 GroupControlTogglePosition.BEFORE
时调整第一列的深度
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
shiftRowGroupDepth(delta)
按指定数量更改范围的行分组深度。
这样可以创建、修改或删除与范围相交的群组。对于正增量,系统会创建和/或修改组;对于负增量,则会销毁和/或修改组。
将组深度降低到零或大于 8 时不起作用。
如果 row group control position
为 BEFORE
,则会在尝试调整第一行的深度时抛出错误。
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; var range = sheet.getActiveRange(); // The row grouping depth is increased by 1. range.shiftRowGroupDepth(1); // The row grouping depth is decreased by 1. range.shiftRowGroupDepth(-1);
参数
名称 | 类型 | 说明 |
---|---|---|
delta | Integer | 更改此范围的行组深度。 |
返程
Range
- 此范围用于串联。
抛出
Error
- 尝试在控件位置为 GroupControlTogglePosition.BEFORE
时调整首行深度
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
sort(sortSpecObj)
按指定的列和顺序对指定范围内的单元格进行排序。
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("A1:C7"); // Sorts by the values in the first column (A) range.sort(1); // Sorts by the values in the second column (B) range.sort(2); // Sorts descending by column B range.sort({column: 2, ascending: false}); // Sorts descending by column B, then ascending by column A // Note the use of an array range.sort([{column: 2, ascending: false}, {column: 1, ascending: true}]); // For rows that are sorted in ascending order, the "ascending" parameter is // optional, and just an integer with the column can be used instead. Note that // in general, keeping the sort specification consistent results in more readable // code. You can express the earlier sort as: range.sort([{column: 2, ascending: false}, 1]); // Alternatively, if you want all columns to be in ascending order, you can use // the following (this makes column 2 ascending) range.sort([2, 1]); // ... which is equivalent to range.sort([{column: 2, ascending: true}, {column: 1, ascending: true}]);
参数
名称 | 类型 | 说明 |
---|---|---|
sortSpecObj | Object | 要排序的列。 |
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
splitTextToColumns()
根据自动检测的分隔符将一列文本拆分为多列。
// A1:A3 has the following values: // A B C // 1 |one,one,one | | | // 2 |two,two,two | | | // 3 |three,three,three| | | var range = SpreadsheetApp.getActiveSheet().getRange("A1:A3"); range.splitTextToColumns(); // Result after spliting the text to columns: // A B C // 1 |one |one |one | // 2 |two |two |two | // 3 |three |three |three |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
splitTextToColumns(delimiter)
将指定的文本字符串作为自定义分隔符将一列文本拆分为多列。
// A1:A3 has the following values: // A B C // 1 |one#one#one | | | // 2 |two#two#two | | | // 3 |three#three#three| | | var range = SpreadsheetApp.getActiveSheet().getRange("A1:A3"); range.splitTextToColumns('#'); // Result after spliting the text to columns: // A B C // 1 |one |one |one | // 2 |two |two |two | // 3 |three |three |three |
参数
名称 | 类型 | 说明 |
---|---|---|
delimiter | String | 要拆分的自定义分隔符。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
splitTextToColumns(delimiter)
根据指定的分隔符将一列文本拆分为多列。
// A1:A3 has the following values: // A B C // 1 |one;one;one | | | // 2 |two;two;two | | | // 3 |three;three;three| | | var range = SpreadsheetApp.getActiveSheet().getRange("A1:A3"); range.splitTextToColumns(SpreadsheetApp.TextToColumnsDelimiter.SEMICOLON); // Result after spliting the text to columns: // A B C // 1 |one |one |one | // 2 |two |two |two | // 3 |three |three |three |
参数
名称 | 类型 | 说明 |
---|---|---|
delimiter | TextToColumnsDelimiter | 要拆分的预设分隔符。 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
trimWhitespace()
修改此范围每个单元格内的空格(例如空格、制表符或换行符)。从每个单元格文本的开头和末尾移除所有空格,并将剩余空白字符的所有子序列减少到一个空格。
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; var range = sheet.getRange('A1:A4'); range.activate(); range.setValues( [' preceding space', 'following space ', 'two middle spaces', ' =SUM(1,2)']) range.trimWhitespace(); var values = range.getValues(); // Values are ['preceding space', 'following space', 'two middle spaces', '=SUM(1,2)']
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
uncheck()
将范围中复选框的状态更改为“未选中”。忽略该范围内当前未配置选中或未选中值的单元格。
// Changes the state of cells which currently contain either the checked or unchecked value // configured in the range A1:B10 to 'unchecked'. var range = SpreadsheetApp.getActive().getRange('A1:B10'); range.uncheck();
返程
Range
- 此范围用于串联。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets