存取及修改 Google 試算表檔案。常見作業包括新增工作表和新增共同編輯者。
方法
內容詳盡的說明文件
add Developer Metadata(key)
將具有指定鍵的開發人員中繼資料新增至頂層試算表。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Adds the key 'NAME' in the developer metadata for the spreadsheet. ss.addDeveloperMetadata('NAME'); // Gets the first developer metadata object and logs its key. const developerMetaData = ss.getDeveloperMetadata()[0]; console.log(developerMetaData.getKey());
參數
名稱 | 類型 | 說明 |
---|---|---|
key | String | 新開發人員中繼資料的鍵。 |
回攻員
Spreadsheet
- 這個試算表,用於串連。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
add Developer Metadata(key, visibility)
將具有指定鍵和瀏覽權限的開發人員中繼資料新增至試算表。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Adds the key 'NAME' in the developer metadata for the spreadsheet and sets // the visibility to the developer project that created the metadata. ss.addDeveloperMetadata( 'NAME', SpreadsheetApp.DeveloperMetadataVisibility.PROJECT, ); // Gets the first developer metadata object and logs its key and visibility // setting. const developerMetaData = ss.getDeveloperMetadata()[0]; console.log(developerMetaData.getKey()); console.log(`Key: ${developerMetaData.getKey()}, . Visibility: ${developerMetaData.getVisibility()}`);
參數
名稱 | 類型 | 說明 |
---|---|---|
key | String | 新開發人員中繼資料的鍵。 |
visibility | Developer | 新開發人員中繼資料的瀏覽權限。 |
回攻員
Spreadsheet
- 這個試算表,用於串連。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
add Developer Metadata(key, value)
將指定鍵和值的開發人員中繼資料新增至試算表。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Adds the key 'NAME' and sets the value to 'GOOGLE' in the developer metadata // for the spreadsheet. ss.addDeveloperMetadata('NAME', 'GOOGLE'); // Gets the first developer metadata object and logs its key and value. const developerMetaData = ss.getDeveloperMetadata()[0]; console.log(developerMetaData.getKey()); console.log( `Key: ${developerMetaData.getKey()}, Value: ${ developerMetaData.getValue()}`, );
參數
名稱 | 類型 | 說明 |
---|---|---|
key | String | 新開發人員中繼資料的鍵。 |
value | String | 新開發人員中繼資料的值。 |
回攻員
Spreadsheet
- 這個試算表,用於串連。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
add Developer Metadata(key, value, visibility)
將指定鍵、值和瀏覽權限的開發人員中繼資料新增至試算表。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Adds the key 'NAME', sets the value to 'GOOGLE', and sets the visibility // to any developer project with document access. ss.addDeveloperMetadata( 'NAME', 'GOOGLE', SpreadsheetApp.DeveloperMetadataVisibility.DOCUMENT, ); // Gets the first developer metadata object and logs its key, value, and // visibility setting. const developerMetaData = ss.getDeveloperMetadata()[0]; console.log(`Key: ${developerMetaData.getKey()}, Value: ${developerMetaData.getValue()}, Visibility: ${developerMetaData.getVisibility()}`);
參數
名稱 | 類型 | 說明 |
---|---|---|
key | String | 新開發人員中繼資料的鍵。 |
value | String | 新開發人員中繼資料的值。 |
visibility | Developer | 新開發人員中繼資料的瀏覽權限。 |
回攻員
Spreadsheet
- 這個試算表,用於串連。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
add Editor(emailAddress)
將指定使用者新增至 Spreadsheet
的編輯者清單。如果使用者已在觀眾名單中,這個方法會將使用者從觀眾名單中移除。
參數
名稱 | 類型 | 說明 |
---|---|---|
email | String | 要新增的使用者電子郵件地址。 |
回攻員
Spreadsheet
— This Spreadsheet
, for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
add Editor(user)
將指定使用者新增至 Spreadsheet
的編輯者清單。如果使用者已在觀眾名單中,這個方法會將使用者從觀眾名單中移除。
參數
名稱 | 類型 | 說明 |
---|---|---|
user | User | 要新增的使用者代表。 |
回攻員
Spreadsheet
— This Spreadsheet
, for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
add Editors(emailAddresses)
將指定的使用者陣列新增至 Spreadsheet
的編輯者清單。如果使用者已在觀眾名單中,這個方法會將他們從觀眾名單中移除。
參數
名稱 | 類型 | 說明 |
---|---|---|
email | String[] | 要新增的使用者電子郵件地址陣列。 |
回攻員
Spreadsheet
— This Spreadsheet
, for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
add Menu(name, subMenus)
在試算表 UI 中建立新選單。
每個選單項目都會執行使用者定義函式。通常您會從 on
函式呼叫這個函式,以便在載入試算表時自動建立選單。
// The onOpen function is executed automatically every time a Spreadsheet is // loaded function onOpen() { const ss = SpreadsheetApp.getActiveSpreadsheet(); const menuEntries = []; // When the user clicks on "addMenuExample" then "Menu Entry 1", the function // function1 is executed. menuEntries.push({name: 'Menu Entry 1', functionName: 'function1'}); menuEntries.push(null); // line separator menuEntries.push({name: 'Menu Entry 2', functionName: 'function2'}); ss.addMenu('addMenuExample', menuEntries); }
參數
名稱 | 類型 | 說明 |
---|---|---|
name | String | 要建立的選單名稱。 |
sub | Object[] | 含有 name 和 function 參數的 JavaScript 地圖陣列。您可以使用內含程式庫中的函式,例如 Library.libFunction1 。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
add Viewer(emailAddress)
將指定使用者新增至 Spreadsheet
的檢視者清單。如果使用者已在編輯者名單中,這個方法不會有任何作用。
參數
名稱 | 類型 | 說明 |
---|---|---|
email | String | 要新增的使用者電子郵件地址。 |
回攻員
Spreadsheet
— This Spreadsheet
, for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
add Viewer(user)
將指定使用者新增至 Spreadsheet
的檢視者清單。如果使用者已在編輯者名單中,這個方法不會有任何作用。
參數
名稱 | 類型 | 說明 |
---|---|---|
user | User | 要新增的使用者代表。 |
回攻員
Spreadsheet
— This Spreadsheet
, for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
add Viewers(emailAddresses)
將指定的使用者陣列新增至 Spreadsheet
的檢視者清單。如果使用者已在編輯者清單中,這個方法對他們不會有任何影響。
參數
名稱 | 類型 | 說明 |
---|---|---|
email | String[] | 要新增的使用者電子郵件地址陣列。 |
回攻員
Spreadsheet
— This Spreadsheet
, for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
append Row(rowContents)
在工作表的目前資料區域底部附加資料列。如果儲存格內容以 =
開頭,系統會將其解讀為公式。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Appends a new row with 3 columns to the bottom of the current // data region in the sheet containing the values in the array. sheet.appendRow(['a man', 'a plan', 'panama']);
參數
名稱 | 類型 | 說明 |
---|---|---|
row | Object[] | 要在工作表最後一列後方插入的值陣列。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
auto Resize Column(columnPosition)
將指定資料欄的寬度設為符合內容。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; sheet.getRange('a1').setValue( 'Whenever it is a damp, drizzly November in my soul...'); // Sets the first column to a width which fits the text sheet.autoResizeColumn(1);
參數
名稱 | 類型 | 說明 |
---|---|---|
column | Integer | 要調整大小的資料欄位置。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
copy(name)
複製試算表並傳回新副本。
// This code makes a copy of the current spreadsheet and names it appropriately const ss = SpreadsheetApp.getActiveSpreadsheet(); ss.copy(`Copy of ${ss.getName()}`);
參數
名稱 | 類型 | 說明 |
---|---|---|
name | String | 副本名稱。 |
回攻員
Spreadsheet
- 這個試算表,用於串連。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
create Developer Metadata Finder()
傳回 Developer
,用於在這個試算表的範圍內尋找開發人員中繼資料。根據預設,這會將與試算表、工作表、列和欄相關聯的所有中繼資料納入考量。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Adds developer metadata to the spreadsheet. ss.addDeveloperMetadata('NAME', 'CHARLIE'); ss.addDeveloperMetadata('COMPANY', 'EXAMPLE ORGANIZATION'); ss.addDeveloperMetadata('TECHNOLOGY', 'JAVASCRIPT'); // Creates a developer metadata finder. const developerMetadataFinder = ss.createDeveloperMetadataFinder(); // Finds the developer metadata objects with 'COMPANY' as the key. const googleMetadataFromSpreadsheet = developerMetadataFinder.withKey('COMPANY').find(); // Gets the first result of developer metadata that has the key 'COMPANY' and // logs its value. console.log(googleMetadataFromSpreadsheet[0].getValue());
回攻員
Developer
- 開發人員中繼資料搜尋工具,可搜尋這個試算表範圍內的中繼資料。
create Text Finder(findText)
為試算表建立文字搜尋器,可用於在試算表中尋找及取代文字。搜尋作業會從試算表的第一張工作表開始。
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // Creates a text finder. const textFinder = spreadsheet.createTextFinder('dog'); // Returns the first occurrence of 'dog' in the spreadsheet. const firstOccurrence = textFinder.findNext(); // Replaces the last found occurrence of 'dog' with 'cat' and returns the number // of occurrences replaced. const numOccurrencesReplaced = textFinder.replaceWith('cat');
參數
名稱 | 類型 | 說明 |
---|---|---|
find | String | 要搜尋的文字。 |
回攻員
Text
- 試算表的 Text
。
delete Active Sheet()
刪除目前使用的工作表。
// The code below deletes the currently active sheet and stores the new active // sheet in a variable const newSheet = SpreadsheetApp.getActiveSpreadsheet().deleteActiveSheet();
回攻員
Sheet
:新的有效工作表。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
delete Column(columnPosition)
刪除指定欄位置的欄。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Columns start at "1" - this deletes the first column sheet.deleteColumn(1);
參數
名稱 | 類型 | 說明 |
---|---|---|
column | Integer | 資料欄位置,第一個資料欄從 1 開始。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
delete Columns(columnPosition, howMany)
從指定欄位置開始刪除多個欄。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Columns start at "1" - this deletes the first two columns sheet.deleteColumns(1, 2);
參數
名稱 | 類型 | 說明 |
---|---|---|
column | Integer | 要刪除的第一欄位置。 |
how | Integer | 要刪除的欄數。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
delete Row(rowPosition)
刪除指定列位置的資料列。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Rows start at "1" - this deletes the first row sheet.deleteRow(1);
參數
名稱 | 類型 | 說明 |
---|---|---|
row | Integer | 資料列的位置,第一列從 1 開始。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
delete Rows(rowPosition, howMany)
從指定資料列位置開始刪除多個資料列。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Rows start at "1" - this deletes the first two rows sheet.deleteRows(1, 2);
參數
名稱 | 類型 | 說明 |
---|---|---|
row | Integer | 要刪除的第一列位置。 |
how | Integer | 要刪除的列數。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
delete Sheet(sheet)
刪除指定的工作表。
// The code below deletes the specified sheet. const ss = SpreadsheetApp.getActive(); const sheet = ss.getSheetByName('My Sheet'); ss.deleteSheet(sheet);
參數
名稱 | 類型 | 說明 |
---|---|---|
sheet | Sheet | 要刪除的工作表。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
duplicate Active Sheet()
get Active Cell()
傳回這個工作表中的作用中儲存格。
注意:建議使用 get
,這個項目會傳回目前醒目顯示的儲存格。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Returns the active cell const cell = sheet.getActiveCell();
回攻員
Range
:目前編輯中的儲存格。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Active Range()
傳回有效工作表中的所選範圍,如果沒有有效範圍,則傳回「null
」。如果選取多個範圍,這個方法只會傳回最後選取的範圍。
「有效範圍」一詞是指使用者在有效工作表中選取的範圍,但在自訂函式中,是指正在重新計算的儲存格。
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); const activeRange = sheet.getActiveRange();
回攻員
Range
:有效範圍。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
另請參閱
get Active Range List()
傳回有效工作表中的有效範圍清單,或在沒有有效範圍時傳回 null
。
如果只選取單一範圍,這項操作的行為與 get
呼叫相同。
const sheet = SpreadsheetApp.getActiveSheet(); // Returns the list of active ranges. const activeRangeList = sheet.getActiveRangeList();
回攻員
Range
- 有效範圍清單。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
另請參閱
get Active Sheet()
取得試算表中的現用工作表。
試算表中的現用工作表是指試算表 UI 中顯示的工作表。
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
回攻員
Sheet
:試算表中的有效工作表。
get As(contentType)
以轉換為指定內容類型的 Blob 形式,傳回這個物件內的資料。這個方法會在檔案名稱中加入適當的副檔名,例如「myfile.pdf」。不過,系統會假設檔案名稱中最後一個半形句點 (如有) 後方的部分是現有副檔名,應予以取代。因此,「ShoppingList.12.25.2014」會變成「ShoppingList.12.25.pdf」。
如要查看每日轉換配額,請參閱「Google 服務配額」。新建立的 Google Workspace 網域可能暫時會受到較嚴格的配額限制。
參數
名稱 | 類型 | 說明 |
---|---|---|
content | String | 要轉換成的 MIME 類型。對於大多數的 Blob,'application/pdf' 是唯一有效的選項。如果是 BMP、GIF、JPEG 或 PNG 格式的圖片,'image/bmp' 、'image/gif' 、'image/jpeg' 或 'image/png' 也有效。如果是 Google 文件,'text/markdown' 也適用。 |
回攻員
Blob
:以 Blob 形式呈現的資料。
get Bandings()
傳回這個試算表中的所有帶狀。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Gets an array of the bandings in the spreadsheet. const bandings = ss.getBandings(); // Logs the range of the first banding in the spreadsheet to the console. console.log(bandings[0].getRange().getA1Notation());
回攻員
Banding[]
:這份試算表中的帶狀範圍。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Blob()
get Column Width(columnPosition)
取得指定資料欄的寬度 (以像素為單位)。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Columns start at 1 Logger.log(sheet.getColumnWidth(1));
參數
名稱 | 類型 | 說明 |
---|---|---|
column | Integer | 要檢查的資料欄位置。 |
回攻員
Integer
:欄寬 (以像素為單位)。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Current Cell()
傳回有效工作表中的目前儲存格,或 null
(如果沒有目前儲存格)。目前儲存格是指 Google 試算表 UI 中具有焦點的儲存格,並以深色邊框醒目顯示。目前儲存格一律只有一個。使用者選取一或多個儲存格範圍時,所選範圍中的其中一個儲存格就是目前的儲存格。
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // Returns the current highlighted cell in the one of the active ranges. const currentCell = sheet.getCurrentCell();
回攻員
Range
:目前的儲存格。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Data Range()
傳回與資料所在維度對應的 Range
。
這在功能上等同於建立以 A1 和 (Sheet.getLastColumn()、Sheet.getLastRow()) 為界的範圍。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // This represents ALL the data const range = sheet.getDataRange(); const values = range.getValues(); // This logs the spreadsheet in CSV format with a trailing comma for (let i = 0; i < values.length; i++) { let row = ''; for (let j = 0; j < values[i].length; j++) { if (values[i][j]) { row = row + values[i][j]; } row = `${row},`; } Logger.log(row); }
回攻員
Range
:包含試算表中所有資料的範圍。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Data Source Formulas()
取得所有資料來源公式。
// Opens the spreadsheet by its ID. If you created your script from within a // Google Sheets file, use SpreadsheetApp.getActiveSpreadsheet(). // TODO(developer): Replace the ID with your own. const ss = SpreadsheetApp.openById('abc123456'); // Gets Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets an array of the data source formulas on Sheet1. // To get an array of data source formulas for the entire spreadsheet, // replace 'sheet' with 'ss'. const dataSourceFormulas = sheet.getDataSourceFormulas(); // Logs the first data source formula in the array. console.log(dataSourceFormulas[0].getFormula());
回攻員
Data
:資料來源公式清單。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Data Source Pivot Tables()
取得所有資料來源資料透視表。
// Opens the spreadsheet file by its ID. If you created your script from a // Google Sheets file, use SpreadsheetApp.getActiveSpreadsheet(). // TODO(developer): Replace the ID with your own. const ss = SpreadsheetApp.openById('abc123456'); // Gets Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets an array of the data source pivot tables on Sheet1. // To get an array of data source pivot tables for the entire // spreadsheet, replace 'sheet' with 'ss'. const dataSourcePivotTables = sheet.getDataSourcePivotTables(); // Logs the last time that the first pivot table in the array was refreshed. console.log(dataSourcePivotTables[0].getStatus().getLastRefreshedTime());
回攻員
Data
- 資料來源資料透視表清單。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Data Source Refresh Schedules()
取得這份試算表的重新整理時間表。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Activates BigQuery operations for the connected spreadsheet. SpreadsheetApp.enableBigQueryExecution(); // Gets the frequency type of the first referesh schedule in the array. const frequencyType = ss.getDataSourceRefreshSchedules()[0] .getFrequency() .getFrequencyType() .toString(); // Logs the frequency type to the console. console.log(frequencyType);
回攻員
Data
:這份試算表的重新整理時間表。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Data Source Sheets()
傳回試算表中的所有資料來源工作表。
// Turns data execution on for BigQuery data sources. SpreadsheetApp.enableBigQueryExecution(); // Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Gets the first data source sheet in the spreadsheet. const dataSource = ss.getDataSourceSheets()[0]; // Gets the name of the data source sheet. console.log(dataSource.asSheet().getName());
回攻員
Data
:所有資料來源工作表的陣列。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Data Source Tables()
取得所有資料來源資料表。
// Opens the spreadsheet file by its ID. If you created your script from a // Google Sheets file, use SpreadsheetApp.getActiveSpreadsheet(). // TODO(developer): Replace the ID with your own. const ss = SpreadsheetApp.openById('abc123456'); // Gets Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets an array of data source tables on Sheet1. // To get an array of data source tables for the entire spreadsheet, // replace 'sheet' with 'ss'. const dataSourceTables = sheet.getDataSourceTables(); // Logs the last completed data execution time on the first data source table. console.log(dataSourceTables[0].getStatus().getLastExecutionTime());
回攻員
Data
:資料來源表格清單。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Data Sources()
傳回試算表中的所有資料來源。
// Turns data execution on for BigQuery data sources. SpreadsheetApp.enableBigQueryExecution(); // Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Gets the data sources on the spreadsheet. const dataSources = ss.getDataSources(); // Logs the name of the first column on the first data source. console.log(dataSources[0].getColumns()[0].getName());
回攻員
Data
:所有資料來源的陣列。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Developer Metadata()
取得與頂層試算表相關聯的開發人員中繼資料。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Adds 'Google' as a key to the spreadsheet metadata. ss.addDeveloperMetadata('Google'); // Gets the spreadsheet's metadata. const ssMetadata = ss.getDeveloperMetadata(); // Gets the first set of the spreadsheet's metadata and logs the key to the // console. console.log(ssMetadata[0].getKey());
回攻員
Developer
:與這個範圍相關聯的開發人員中繼資料。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Editors()
取得這個 Spreadsheet
的編輯者清單。
回攻員
User[]
:具有編輯權限的使用者陣列。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Form Url()
傳回將回覆傳送至這個試算表的表單網址,如果這個試算表沒有相關聯的表單,則傳回 null
。如果多份表單將回覆傳送至這個試算表,系統傳回的表單網址將不確定。或者,您也可以透過 Sheet.getFormUrl()
方法,擷取每個工作表的表單網址關聯。如果使用者沒有編輯試算表的權限,系統會擲回例外狀況。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Gets the form URL from the spreadsheet. const formUrl = ss.getFormUrl(); // Logs the form URL to the console. console.log(formUrl);
回攻員
String
:將回覆內容匯入這個試算表的表單網址,或 null
(如果這個試算表沒有相關聯的表單)。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Frozen Columns()
傳回凍結的欄數。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; Logger.log('Number of frozen columns: %s', sheet.getFrozenColumns());
回攻員
Integer
:凍結的欄數。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Frozen Rows()
傳回凍結的列數。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; Logger.log('Number of frozen rows: %s', sheet.getFrozenRows());
回攻員
Integer
:凍結列數。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Id()
取得這個試算表的專屬 ID。試算表 ID 可從網址中擷取。 舉例來說,在網址 https://docs.google.com/spreadsheets/d/abc1234567/edit#gid=0 中,試算表 ID 為「abc1234567」。
// The code below logs the ID for the active spreadsheet. Logger.log(SpreadsheetApp.getActiveSpreadsheet().getId());
回攻員
String
:試算表的專屬 ID (或鍵)。
get Images()
傳回工作表上所有超出格線的圖片。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Gets Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets the over-the-grid images from Sheet1. // To get the over-the-grid images from the entire spreadsheet, use // ss.getImages() instead. const images = sheet.getImages(); // For each image, logs the anchor cell in A1 notation. for (const image of images) { console.log(image.getAnchorCell().getA1Notation()); }
回攻員
Over
:格線上方圖片陣列。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Iterative Calculation Convergence Threshold()
傳回疊代計算期間使用的閾值。如果連續計算的結果差異小於這個值,系統就會停止疊代計算。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Sets the iterative calculation convergence threshold for the spreadsheet. ss.setIterativeCalculationConvergenceThreshold(2); // Logs the threshold to the console. console.log(ss.getIterativeCalculationConvergenceThreshold());
回攻員
Number
- 收斂門檻。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Last Column()
傳回含有內容的最後一欄位置。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // This logs the value in the very last cell of this sheet const lastRow = sheet.getLastRow(); const lastColumn = sheet.getLastColumn(); const lastCell = sheet.getRange(lastRow, lastColumn); Logger.log(lastCell.getValue());
回攻員
Integer
:包含內容的試算表最後一欄。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Last Row()
傳回含有內容的最後一列位置。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // This logs the value in the very last cell of this sheet const lastRow = sheet.getLastRow(); const lastColumn = sheet.getLastColumn(); const lastCell = sheet.getRange(lastRow, lastColumn); Logger.log(lastCell.getValue());
回攻員
Integer
:包含內容的試算表最後一列。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Max Iterative Calculation Cycles()
傳回疊代計算期間使用的疊代次數上限。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Sets the max iterative calculation cycles for the spreadsheet. ss.setMaxIterativeCalculationCycles(10); // Logs the max iterative calculation cycles to the console. console.log(ss.getMaxIterativeCalculationCycles());
回攻員
Integer
:計算疊代次數上限。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Name()
取得文件的名稱。
const ss = SpreadsheetApp.getActiveSpreadsheet(); Logger.log(ss.getName());
回攻員
String
:試算表名稱。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Named Ranges()
取得這個試算表中的所有已命名範圍。
// The code below logs the name of the first named range. const namedRanges = SpreadsheetApp.getActiveSpreadsheet().getNamedRanges(); for (let i = 0; i < namedRanges.length; i++) { Logger.log(namedRanges[i].getName()); }
回攻員
Named
:試算表中所有已命名範圍的陣列。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Num Sheets()
傳回試算表中的工作表數量。
// The code below logs the number of sheets in the active spreadsheet. Logger.log(SpreadsheetApp.getActiveSpreadsheet().getNumSheets());
回攻員
Integer
- 試算表中的工作表數量。
get Owner()
傳回文件擁有者,或共用雲端硬碟中文件的 null
。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const owner = ss.getOwner(); Logger.log(owner.getEmail());
回攻員
User
- 文件擁有者,或 null
(如果文件位於共用雲端硬碟中)。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Predefined Spreadsheet Themes()
傳回預先定義的主題清單。
// The code below returns the list of predefined themes. const predefinedThemesList = SpreadsheetApp.getActiveSpreadsheet().getPredefinedSpreadsheetThemes();
回攻員
Spreadsheet
:預先定義的主題清單。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Protections(type)
取得代表試算表中所有受保護範圍或工作表的物件陣列。
// Remove all range protections in the spreadsheet that the user has permission // to edit. const ss = SpreadsheetApp.getActive(); const protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (let i = 0; i < protections.length; i++) { const protection = protections[i]; if (protection.canEdit()) { protection.remove(); } }
// Remove all sheet protections in the spreadsheet that the user has permission // to edit. const ss = SpreadsheetApp.getActive(); const protections = ss.getProtections(SpreadsheetApp.ProtectionType.SHEET); for (let i = 0; i < protections.length; i++) { const protection = protections[i]; if (protection.canEdit()) { protection.remove(); } }
參數
名稱 | 類型 | 說明 |
---|---|---|
type | Protection | 受保護區域的類型,可以是 Spreadsheet 或 Spreadsheet 。 |
回攻員
Protection[]
- 代表試算表中所有受保護範圍或工作表的物件陣列。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Range(a1Notation)
以 A1 或 R1C1 標記法傳回指定範圍。
// Get a range A1:D4 on sheet titled "Invoices" const ss = SpreadsheetApp.getActiveSpreadsheet(); const range = ss.getRange('Invoices!A1:D4'); // Get cell A1 on the first sheet const sheet = ss.getSheets()[0]; const cell = sheet.getRange('A1');
參數
名稱 | 類型 | 說明 |
---|---|---|
a1Notation | String | 要傳回的範圍,以 A1 標記法或 R1C1 標記法指定。 |
回攻員
Range
- 指定地點的範圍。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Range By Name(name)
傳回具名範圍,或如果找不到具有指定名稱的範圍,則傳回 null
。如果試算表的多個工作表使用相同的範圍名稱,請指定工作表名稱,不要加上額外的引號,例如 get
或 get
,但不能是 get
。
// Log the number of columns for the range named 'TaxRates' in the active // spreadsheet. const range = SpreadsheetApp.getActiveSpreadsheet().getRangeByName('TaxRates'); if (range != null) { Logger.log(range.getNumColumns()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
name | String | 要取得的範圍名稱。 |
回攻員
Range
:具有指定名稱的儲存格範圍。
get Range List(a1Notations)
傳回 Range
集合,代表同一工作表中以非空白 A1 標記或 R1C1 標記清單指定的範圍。
// Get a list of ranges A1:D4, F1:H4. const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); const rangeList = sheet.getRangeList(['A1:D4', 'F1:H4']);
參數
名稱 | 類型 | 說明 |
---|---|---|
a1Notations | String[] | 要傳回的範圍清單,以 A1 標記法或 R1C1 標記法指定。 |
回攻員
Range
- 指定位置的範圍清單。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Recalculation Interval()
傳回這份試算表的計算間隔。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Logs the calculation interval for the spreadsheet to the console. console.log(ss.getRecalculationInterval().toString());
回攻員
Recalculation
:這份試算表的計算間隔。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Row Height(rowPosition)
取得指定列的高度 (以像素為單位)。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Rows start at 1 Logger.log(sheet.getRowHeight(1));
參數
名稱 | 類型 | 說明 |
---|---|---|
row | Integer | 要檢查的資料列位置。 |
回攻員
Integer
:列高 (以像素為單位)。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Selection()
get Sheet By Id(id)
取得具有指定 ID 的工作表。使用 Sheet.getSheetId()
。
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetById(12345);
參數
名稱 | 類型 | 說明 |
---|---|---|
id | Integer | 要取得的工作表 ID。 |
回攻員
Sheet
:具有指定 ID 的工作表,或如果找不到工作表,則為 null
。
get Sheet By Name(name)
傳回指定名稱的工作表。
如果多個工作表名稱相同,系統會傳回最左側的工作表。如果沒有指定名稱的工作表,則傳回 null
。
// The code below logs the index of a sheet named "Expenses" const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Expenses'); if (sheet != null) { Logger.log(sheet.getIndex()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
name | String | 要取得的試算表名稱。 |
回攻員
Sheet
:指定名稱的工作表。
get Sheet Id()
傳回這個物件代表的試算表 ID。
這是試算表的專屬工作表 ID。這個 ID 是在建立工作表時指派的單調遞增整數,與工作表位置無關。這項功能可搭配 Range.copyFormatToRange(gridId, column, columnEnd, row, rowEnd)
等方法使用,這些方法會採用 grid
參數,而非 Sheet
例項。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; Logger.log(sheet.getSheetId());
回攻員
Integer
:試算表專屬的工作表 ID。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Sheet Name()
傳回工作表名稱。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; Logger.log(sheet.getSheetName());
回攻員
String
:工作表的名稱。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Sheet Values(startRow, startColumn, numRows, numColumns)
傳回這個範圍的值矩形格線,從指定座標開始。如果列或欄位置的值為 -1,則相當於取得工作表中有資料的最後一列或最後一欄。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // The two samples below produce the same output let values = sheet.getSheetValues(1, 1, 3, 3); Logger.log(values); const range = sheet.getRange(1, 1, 3, 3); values = range.getValues(); Logger.log(values);
參數
名稱 | 類型 | 說明 |
---|---|---|
start | Integer | 起始列的位置。 |
start | Integer | 起始資料欄的位置。 |
num | Integer | 要傳回值的列數。 |
num | Integer | 要傳回值的欄數。 |
回攻員
Object[][]
:值的二維陣列。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Sheets()
取得這個試算表中的所有工作表。
// The code below logs the name of the second sheet const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets(); // Iterates through the sheets and logs the name and ID of each sheet. for (const sheet of sheets) { Logger.log(`name: ${sheet.getName()}, ID: ${sheet.getSheetId()}`); }
回攻員
Sheet[]
:試算表中所有工作表的陣列。
get Spreadsheet Locale()
取得試算表語言代碼。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Gets the spreadsheet locale. const ssLocale = ss.getSpreadsheetLocale(); // Logs the locale to the console. console.log(ssLocale);
回攻員
String
- 試算表語言代碼。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Spreadsheet Theme()
傳回試算表的目前主題,如果未套用任何主題,則傳回 null
。
// The code below returns the current theme of the spreadsheet. const currentTheme = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTheme();
回攻員
Spreadsheet
:目前套用的主題。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Spreadsheet Time Zone()
取得試算表的時區。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Sets the time zone of the spreadsheet. ss.setSpreadsheetTimeZone('America/New_York'); // Gets the time zone of the spreadsheet. const ssTimeZone = ss.getSpreadsheetTimeZone(); // Logs the time zone to the console. console.log(ssTimeZone);
回攻員
String
:時區,以「long」格式指定 (例如「America/New_York」,如 Joda.org 所列)。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Url()
傳回指定試算表的網址。
const ss = SpreadsheetApp.getActiveSpreadsheet(); Logger.log(ss.getUrl());
回攻員
String
:指定試算表的網址。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
get Viewers()
取得這個 Spreadsheet
的檢視者和加註者清單。
回攻員
User[]
:具有檢視或註解權限的使用者陣列。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
hide Column(column)
隱藏指定範圍中的欄。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // This hides the first column let range = sheet.getRange('A1'); sheet.hideColumn(range); // This hides the first 3 columns range = sheet.getRange('A:C'); sheet.hideColumn(range);
參數
名稱 | 類型 | 說明 |
---|---|---|
column | Range | 要隱藏的欄範圍。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
hide Row(row)
隱藏指定範圍內的資料列。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // This hides the first row const range = sheet.getRange('A1'); sheet.hideRow(range);
參數
名稱 | 類型 | 說明 |
---|---|---|
row | Range | 要隱藏的資料列範圍。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Column After(afterPosition)
在指定資料欄位置後插入資料欄。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // This inserts a column after the first column position sheet.insertColumnAfter(1);
參數
名稱 | 類型 | 說明 |
---|---|---|
after | Integer | 要在其後方新增資料欄的資料欄。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Column Before(beforePosition)
在指定資料欄位置之前插入資料欄。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // This inserts a column in the first column position sheet.insertColumnBefore(1);
參數
名稱 | 類型 | 說明 |
---|---|---|
before | Integer | 要新增資料欄的左側資料欄。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Columns After(afterPosition, howMany)
在指定欄位置後插入指定數量的欄。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Inserts two columns after the first column on the first sheet of the // spreadsheet. sheet.insertColumnsAfter(1, 2);
參數
名稱 | 類型 | 說明 |
---|---|---|
after | Integer | 要在其後方新增資料欄的資料欄。 |
how | Integer | 要插入的欄數。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Columns Before(beforePosition, howMany)
在指定欄位置之前插入多個欄。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // This inserts five columns before the first column sheet.insertColumnsBefore(1, 5);
參數
名稱 | 類型 | 說明 |
---|---|---|
before | Integer | 要新增資料欄的左側資料欄。 |
how | Integer | 要插入的欄數。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Data Source Sheet(spec)
在試算表中插入新的 Data
,並開始執行資料。這項操作也會一併將新工作表設為有效工作表。
如果未啟用資料來源類型,系統會擲回例外狀況。使用 Spreadsheet
方法,為特定資料來源類型啟用資料執行程序。
// Activates BigQuery operations. SpreadsheetApp.enableBigQueryExecution(); // Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Builds a data source specification. // TODO (developer): Update the project ID to your own Google Cloud project ID. const dataSourceSpec = SpreadsheetApp.newDataSourceSpec() .asBigQuery() .setProjectId('project-id-1') .setTableProjectId('bigquery-public-data') .setDatasetId('ncaa_basketball') .setTableId('mbb_historical_teams_games') .build(); // Adds the data source and its data to the spreadsheet. ss.insertDataSourceSheet(dataSourceSpec);
參數
名稱 | 類型 | 說明 |
---|---|---|
spec | Data | 要插入的資料來源規格。 |
回攻員
Data
:新的資料來源工作表。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Image(blobSource, column, row)
在指定列和欄中,將 Spreadsheet
做為圖片插入文件中。系統會從 Blob 內容擷取圖片大小。支援的 blob 大小上限為 2 MB。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; const binaryData = []; // TODO(developer): Replace with your binary data. const blob = Utilities.newBlob(binaryData, 'image/png', 'MyImageName'); sheet.insertImage(blob, 1, 1);
參數
名稱 | 類型 | 說明 |
---|---|---|
blob | Blob | 包含圖片內容、MIME 類型和 (選填) 名稱的 Blob。 |
column | Integer | 資料欄位置。 |
row | Integer | 資料列位置。 |
回攻員
Over
:插入的圖片。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Image(blobSource, column, row, offsetX, offsetY)
在文件的指定列和欄中插入 Spreadsheet
做為圖片,並設定像素偏移量。系統會從 Blob 內容擷取圖片大小。支援的 blob 大小上限為 2 MB。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; const binaryData = []; // TODO(developer): Replace with your binary data. const blob = Utilities.newBlob(binaryData, 'image/png', 'MyImageName'); sheet.insertImage(blob, 1, 1, 10, 10);
參數
名稱 | 類型 | 說明 |
---|---|---|
blob | Blob | 包含圖片內容、MIME 類型和 (選填) 名稱的 Blob。 |
column | Integer | 資料欄位置。 |
row | Integer | 資料列位置。 |
offsetX | Integer | 以像素為單位的儲存格角落水平偏移。 |
offsetY | Integer | 以像素為單位的儲存格角落垂直偏移。 |
回攻員
Over
:插入的圖片。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Image(url, column, row)
在指定列和欄的文件中插入圖片。
提供的網址必須可供公開存取。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; sheet.insertImage('https://www.google.com/images/srpr/logo3w.png', 1, 1);
參數
名稱 | 類型 | 說明 |
---|---|---|
url | String | 圖片的網址。 |
column | Integer | 格線欄位置。 |
row | Integer | 格線列位置。 |
回攻員
Over
:插入的圖片。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Image(url, column, row, offsetX, offsetY)
在指定列和欄中插入圖片,並設定像素偏移量。
提供的網址必須可供公開存取。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; sheet.insertImage( 'https://www.google.com/images/srpr/logo3w.png', 1, 1, 10, 10, );
參數
名稱 | 類型 | 說明 |
---|---|---|
url | String | 圖片的網址。 |
column | Integer | 資料欄位置。 |
row | Integer | 資料列位置。 |
offsetX | Integer | 以像素為單位的儲存格角落水平偏移。 |
offsetY | Integer | 以像素為單位的儲存格角落垂直偏移。 |
回攻員
Over
- 插入的圖片。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Row After(afterPosition)
在指定資料列位置後方插入資料列。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // This inserts a row after the first row position sheet.insertRowAfter(1);
參數
名稱 | 類型 | 說明 |
---|---|---|
after | Integer | 要新增資料列的後方資料列。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Row Before(beforePosition)
在指定列位置之前插入列。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // This inserts a row before the first row position sheet.insertRowBefore(1);
參數
名稱 | 類型 | 說明 |
---|---|---|
before | Integer | 新資料列要插入的資料列。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Rows After(afterPosition, howMany)
在指定資料列位置後插入多列。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // This inserts five rows after the first row sheet.insertRowsAfter(1, 5);
參數
名稱 | 類型 | 說明 |
---|---|---|
after | Integer | 新資料列應新增至此資料列之後。 |
how | Integer | 要插入的列數。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Rows Before(beforePosition, howMany)
在指定資料列位置之前插入多個資料列。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // This inserts five rows before the first row sheet.insertRowsBefore(1, 5);
參數
名稱 | 類型 | 說明 |
---|---|---|
before | Integer | 要新增資料列的資料列。 |
how | Integer | 要插入的列數。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Sheet()
insert Sheet(sheetIndex)
在指定索引處將新工作表插入試算表。新工作表會成為有效工作表。
const ss = SpreadsheetApp.getActiveSpreadsheet(); ss.insertSheet(1);
參數
名稱 | 類型 | 說明 |
---|---|---|
sheet | Integer | 新建立的工作表索引。如要在試算表中插入第一個工作表,請將這個值設為 0。 |
回攻員
Sheet
:新工作表。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Sheet(sheetIndex, options)
在指定索引處將新工作表插入試算表,並使用選用的進階引數。新工作表會成為有效工作表。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const templateSheet = ss.getSheetByName('Sales'); ss.insertSheet(1, {template: templateSheet});
參數
名稱 | 類型 | 說明 |
---|---|---|
sheet | Integer | 新建立的工作表索引。如要在試算表中插入第一個工作表,請將這個值設為 0。 |
options | Object | 選用的 JavaScript 進階引數。 |
進階參數
名稱 | 類型 | 說明 |
---|---|---|
template | Sheet | 範本工作表物件中的所有資料都會複製到新工作表。工作表範本必須是試算表物件的工作表之一。 |
回攻員
Sheet
:新工作表。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Sheet(options)
在試算表中插入新工作表,使用預設工作表名稱和選用的進階引數。新工作表會成為有效工作表。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const templateSheet = ss.getSheetByName('Sales'); ss.insertSheet({template: templateSheet});
參數
名稱 | 類型 | 說明 |
---|---|---|
options | Object | 選用 JavaScript 進階引數,如下所示。 |
進階參數
名稱 | 類型 | 說明 |
---|---|---|
template | Sheet | 範本工作表物件中的所有資料都會複製到新工作表。工作表範本必須是這個試算表物件的工作表之一。 |
回攻員
Sheet
:新工作表。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Sheet(sheetName)
insert Sheet(sheetName, sheetIndex)
在試算表的指定索引處,插入具有指定名稱的新工作表。新工作表會成為使用中的工作表。
const ss = SpreadsheetApp.getActiveSpreadsheet(); ss.insertSheet('My New Sheet', 1);
參數
名稱 | 類型 | 說明 |
---|---|---|
sheet | String | 新工作表的名稱。 |
sheet | Integer | 新建立的工作表索引。如要在試算表中插入第一個工作表,請將這個值設為 0。 |
回攻員
Sheet
:新工作表。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Sheet(sheetName, sheetIndex, options)
在試算表的指定索引處插入指定名稱的新工作表,並使用選用的進階引數。新工作表會成為有效工作表。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const templateSheet = ss.getSheetByName('Sales'); ss.insertSheet('My New Sheet', 1, {template: templateSheet});
參數
名稱 | 類型 | 說明 |
---|---|---|
sheet | String | 新工作表的名稱。 |
sheet | Integer | 新插入工作表的索引。如要將工作表插入試算表的第一個位置,請將這個值設為 0。 |
options | Object | 選用的 JavaScript 進階引數。 |
進階參數
名稱 | 類型 | 說明 |
---|---|---|
template | Sheet | 範本工作表物件中的所有資料都會複製到新工作表。工作表範本必須是試算表物件的工作表之一。 |
回攻員
Sheet
:新工作表。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Sheet(sheetName, options)
在試算表中插入指定名稱的新工作表,並使用選用的進階引數。新工作表會成為有效工作表。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const templateSheet = ss.getSheetByName('Sales'); ss.insertSheet('My New Sheet', {template: templateSheet});
參數
名稱 | 類型 | 說明 |
---|---|---|
sheet | String | 新工作表的名稱。 |
options | Object | 選用的 JavaScript 進階引數。 |
進階參數
名稱 | 類型 | 說明 |
---|---|---|
template | Sheet | 範本工作表物件中的所有資料都會複製到新工作表。工作表範本必須是試算表物件的工作表之一。 |
回攻員
Sheet
:新工作表。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
insert Sheet With Data Source Table(spec)
在試算表中插入新工作表,使用指定的資料來源規格建立涵蓋整個工作表的 Data
,然後開始執行資料。並將新工作表設為使用中工作表。
如果未啟用資料來源類型,系統會擲回例外狀況。使用 Spreadsheet
方法,為特定資料來源類型啟用資料執行程序。
// Activates BigQuery operations. SpreadsheetApp.enableBigQueryExecution(); // Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Adds a sheet and sets cell A1 as the parameter cell. const parameterCell = ss.insertSheet('parameterSheet').getRange('A1'); // Sets the value of the parameter cell to 'Duke'. parameterCell.setValue('Duke'); const query = 'select * from `bigquery-public-data`.`ncaa_basketball`.' + '`mbb_historical_tournament_games` WHERE win_school_ncaa = @SCHOOL'; // Adds a data source with a query parameter. // TODO(developer): Update the project ID to your own Google Cloud project ID. const dataSourceSpec = SpreadsheetApp.newDataSourceSpec() .asBigQuery() .setProjectId('project-id-1') .setRawQuery(query) .setParameterFromCell('SCHOOL', 'parameterSheet!A1') .build(); // Adds sheets for the data source and data source table to the spreadsheet. ss.insertSheetWithDataSourceTable(dataSourceSpec);
參數
名稱 | 類型 | 說明 |
---|---|---|
spec | Data | 要插入的資料來源規格。 |
回攻員
Sheet
:新工作表。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
is Column Hidden By User(columnPosition)
傳回指定資料欄是否遭使用者隱藏。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Columns start at 1 Logger.log(sheet.isColumnHiddenByUser(1));
參數
名稱 | 類型 | 說明 |
---|---|---|
column | Integer | 要檢查的資料欄位置。 |
回攻員
Boolean
:如果資料欄已隱藏,則為 true
,否則為 false
。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
is Iterative Calculation Enabled()
傳回這個試算表中是否已啟用反覆運算。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Activates iterative calculation on the spreadsheet. ss.setIterativeCalculationEnabled(true); // Logs whether iterative calculation is activated for the spreadsheet. console.log(ss.isIterativeCalculationEnabled());
回攻員
Boolean
- true
(如果啟用反覆運算) 或 false
(如果未啟用反覆運算)。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
is Row Hidden By Filter(rowPosition)
傳回指定資料列是否因篩選條件 (而非篩選器檢視畫面) 而隱藏。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Rows start at 1 Logger.log(sheet.isRowHiddenByFilter(1));
參數
名稱 | 類型 | 說明 |
---|---|---|
row | Integer | 要檢查的資料列位置。 |
回攻員
Boolean
:如果資料列已隱藏,則為 true
,否則為 false
。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
is Row Hidden By User(rowPosition)
傳回指定資料列是否已遭使用者隱藏。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Rows start at 1 Logger.log(sheet.isRowHiddenByUser(1));
參數
名稱 | 類型 | 說明 |
---|---|---|
row | Integer | 要檢查的資料列位置。 |
回攻員
Boolean
:如果資料列已隱藏,則為 true
,否則為 false
。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
move Active Sheet(pos)
將目前的工作表移至工作表清單中的指定位置。如果位置是負數或大於工作表數量,就會擲回例外狀況。
// This example assumes that there are 2 sheets in the current // active spreadsheet: one named "first" in position 1 and another named // "second" in position 2. const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // Gets the "first" sheet and activates it. const sheet = spreadsheet.getSheetByName('first').activate(); // Logs 'Current index of sheet: 1' console.log('Current index of sheet: %s', sheet.getIndex()); spreadsheet.moveActiveSheet(2); // Logs 'New index of sheet: 2' console.log('New index of sheet: %s', sheet.getIndex());
參數
名稱 | 類型 | 說明 |
---|---|---|
pos | Integer | 要將現用工作表移至工作表清單中的位置 (從 1 開始計算)。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
move Chart To Object Sheet(chart)
建立新的 Sheet
工作表,並將提供的圖表移至該工作表。如果圖表已位於專屬工作表,系統會傳回該工作表,不會建立新工作表。
const sheet = SpreadsheetApp.getActiveSheet(); const chart = sheet.newChart().setPosition(1, 1, 0, 0).build(); sheet.insertChart(chart); const objectSheet = SpreadsheetApp.getActive().moveChartToObjectSheet(chart);
參數
名稱 | 類型 | 說明 |
---|---|---|
chart | Embedded | 要移動的圖表。 |
回攻員
Sheet
:圖表所在的試算表。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
refresh All Data Sources()
重新整理所有支援的資料來源及其連結的資料來源物件,並略過無效的資料來源物件。
使用 Spreadsheet
方法,為特定資料來源類型啟用資料執行程序。
// Activates BigQuery operations. SpreadsheetApp.enableBigQueryExecution(); // Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Gets the first data source sheet on the spreadsheet. const dataSheet = ss.getDataSourceSheets()[0]; // Refreshes all data sources on the spreadsheet. ss.refreshAllDataSources(); // Logs the last refreshed time of the first data source sheet. console.log( `Last refresh time: ${dataSheet.getStatus().getLastRefreshedTime()}`, );
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
remove Editor(emailAddress)
從 Spreadsheet
的編輯者清單中移除指定使用者。如果使用者屬於具有一般存取權的使用者類別,例如 Spreadsheet
是與使用者的整個網域共用,或是 Spreadsheet
位於使用者可存取的共用雲端硬碟中,則這個方法不會禁止使用者存取 Spreadsheet
。
如果是雲端硬碟檔案,系統也會從檢視者清單中移除使用者。
參數
名稱 | 類型 | 說明 |
---|---|---|
email | String | 要移除的使用者電子郵件地址。 |
回攻員
Spreadsheet
— This Spreadsheet
, for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
remove Editor(user)
從 Spreadsheet
的編輯者清單中移除指定使用者。如果使用者屬於具有一般存取權的使用者類別,例如 Spreadsheet
是與使用者的整個網域共用,或是 Spreadsheet
位於使用者可存取的共用雲端硬碟中,則這個方法不會禁止使用者存取 Spreadsheet
。
如果是雲端硬碟檔案,系統也會從檢視者清單中移除使用者。
參數
名稱 | 類型 | 說明 |
---|---|---|
user | User | 要移除的使用者代表。 |
回攻員
Spreadsheet
— This Spreadsheet
, for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
remove Menu(name)
移除 add
新增的選單。name
引數的值應與對應的 add
呼叫相同。
// The onOpen function is executed automatically every time a Spreadsheet is // loaded function onOpen() { const ss = SpreadsheetApp.getActiveSpreadsheet(); ss.addMenu('badMenu', [ {name: 'remove bad menu', functionName: 'removeBadMenu'}, {name: 'foo', functionName: 'foo'}, ]); } function removeBadMenu() { const ss = SpreadsheetApp.getActiveSpreadsheet(); ss.removeMenu( 'badMenu'); // name must match the name used when added the menu } function foo() { // Do nothing }
參數
名稱 | 類型 | 說明 |
---|---|---|
name | String | 要移除的選單名稱。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
remove Named Range(name)
刪除指定名稱的已命名範圍。如果試算表中沒有符合指定名稱的範圍,系統會擲回例外狀況。
// The code below creates a new named range "foo", and then remove it. const ss = SpreadsheetApp.getActiveSpreadsheet(); ss.setNamedRange('foo', ss.getActiveRange()); ss.removeNamedRange('foo');
參數
名稱 | 類型 | 說明 |
---|---|---|
name | String | 範圍名稱。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
remove Viewer(emailAddress)
從 Spreadsheet
的觀眾和留言者清單中移除指定使用者。如果使用者是編輯者,而非檢視者或留言者,這個方法不會起任何作用。如果使用者屬於具有一般存取權的使用者類別 (例如 Spreadsheet
與使用者的整個網域共用,或 Spreadsheet
位於使用者可存取的共用雲端硬碟中),這個方法也無法禁止使用者存取 Spreadsheet
。
如果是雲端硬碟檔案,系統也會從編輯者清單中移除使用者。
參數
名稱 | 類型 | 說明 |
---|---|---|
email | String | 要移除的使用者電子郵件地址。 |
回攻員
Spreadsheet
— This Spreadsheet
for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
remove Viewer(user)
從 Spreadsheet
的觀眾和留言者清單中移除指定使用者。如果使用者是編輯者而非檢視者,這個方法就不會生效。如果使用者屬於具有一般存取權的使用者類別 (例如 Spreadsheet
與使用者的整個網域共用,或 Spreadsheet
位於使用者可存取的共用雲端硬碟中),這個方法也無法禁止使用者存取 Spreadsheet
。
如果是雲端硬碟檔案,系統也會從編輯者清單中移除使用者。
參數
名稱 | 類型 | 說明 |
---|---|---|
user | User | 要移除的使用者代表。 |
回攻員
Spreadsheet
— This Spreadsheet
for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
rename(newName)
重新命名文件。
const ss = SpreadsheetApp.getActiveSpreadsheet(); ss.rename('This is the new name');
參數
名稱 | 類型 | 說明 |
---|---|---|
new | String | 文件的新名稱。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
rename Active Sheet(newName)
將目前作用中的工作表重新命名為指定的新名稱。
// The code below renames the active sheet to "Hello world" SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet('Hello world');
參數
名稱 | 類型 | 說明 |
---|---|---|
new | String | 目前有效工作表的名稱。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
reset Spreadsheet Theme()
移除已套用的主題,並在試算表上設定預設主題。
// The code below applies default theme on the spreadsheet. SpreadsheetApp.getActiveSpreadsheet().resetSpreadsheetTheme();
回攻員
Spreadsheet
:預設主題。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Active Range(range)
將指定範圍設為作用中工作表中的 active range
,且範圍左上角的儲存格為 current cell
。
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); const range = sheet.getRange('A1:D4'); sheet.setActiveRange(range); const selection = sheet.getSelection(); // Current cell: A1 const currentCell = selection.getCurrentCell(); // Active Range: A1:D4 const activeRange = selection.getActiveRange();
參數
名稱 | 類型 | 說明 |
---|---|---|
range | Range | 要設為有效範圍的範圍。 |
回攻員
Range
:新啟用的範圍。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Active Range List(rangeList)
將指定範圍清單設為作用中工作表中的 active ranges
。清單中的最後一個範圍會設為 active range
。
const sheet = SpreadsheetApp.getActiveSheet(); const rangeList = sheet.getRangeList(['D4', 'B2:C4']); sheet.setActiveRangeList(rangeList); const selection = sheet.getSelection(); // Current cell: B2 const currentCell = selection.getCurrentCell(); // Active range: B2:C4 const activeRange = selection.getActiveRange(); // Active range list: [D4, B2:C4] const activeRangeList = selection.getActiveRangeList();
參數
名稱 | 類型 | 說明 |
---|---|---|
range | Range | 可選取的範圍清單。 |
回攻員
Range
:新選取的範圍清單。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Active Selection(range)
為這個工作表設定有效選取區域。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; const range = sheet.getRange('A1:D4'); sheet.setActiveSelection(range);
參數
名稱 | 類型 | 說明 |
---|---|---|
range | Range | 要設為有效選取範圍的範圍。 |
回攻員
Range
:新啟用的範圍。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Active Selection(a1Notation)
設定以 A1 標記法或 R1C1 標記法指定的作用中選取範圍。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; sheet.setActiveSelection('A1:D4');
參數
名稱 | 類型 | 說明 |
---|---|---|
a1Notation | String | 要設為作用中的範圍,以 A1 標記法或 R1C1 標記法指定。 |
回攻員
Range
:新啟用的範圍。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Active Sheet(sheet)
將指定的工作表設為試算表中的有效工作表。除非所選工作表屬於其他試算表,否則 Google 試算表 UI 會顯示所選工作表。
// The code below makes the first sheet active in the active spreadsheet. const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); spreadsheet.setActiveSheet(spreadsheet.getSheets()[0]);
參數
名稱 | 類型 | 說明 |
---|---|---|
sheet | Sheet | 要設為有效工作表的工作表。 |
回攻員
Sheet
:目前使用中的工作表。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Active Sheet(sheet, restoreSelection)
將指定的工作表設為試算表中的現用工作表,並可選擇還原該工作表最近一次的選取範圍。Google 試算表 UI 會顯示所選工作表,除非該工作表屬於其他試算表。
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); const firstSheet = spreadsheet.getSheets()[0]; const secondSheet = spreadsheet.getSheets()[1]; // Set the first sheet as the active sheet and select the range D4:F4. spreadsheet.setActiveSheet(firstSheet).getRange('D4:F4').activate(); // Switch to the second sheet to do some work. spreadsheet.setActiveSheet(secondSheet); // Switch back to first sheet, and restore its selection. spreadsheet.setActiveSheet(firstSheet, true); // The selection of first sheet is restored, and it logs D4:F4 const range = spreadsheet.getActiveSheet().getSelection().getActiveRange(); Logger.log(range.getA1Notation());
參數
名稱 | 類型 | 說明 |
---|---|---|
sheet | Sheet | 新的現用工作表。 |
restore | Boolean | 如果為 true ,系統會在新工作表啟用時,再次選取您最近選取的新工作表;如果為 false ,系統會啟用新工作表,但不會變更目前選取項目。 |
回攻員
Sheet
:新的有效工作表。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Column Width(columnPosition, width)
以像素為單位設定指定欄的寬度。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Sets the first column to a width of 200 pixels sheet.setColumnWidth(1, 200);
參數
名稱 | 類型 | 說明 |
---|---|---|
column | Integer | 要設定的指定資料欄位置。 |
width | Integer | 要設定的寬度 (以像素為單位)。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Current Cell(cell)
將指定儲存格設為 current cell
。
如果指定儲存格位於已選取的範圍中,該範圍就會成為有效範圍,且該儲存格會成為目前儲存格。
如果指定儲存格不在任何選取範圍內,系統會移除所有現有選取項目,並將該儲存格設為目前儲存格和有效範圍。
注意:指定的 Range
必須由一個儲存格組成,否則會擲回例外狀況。
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); const cell = sheet.getRange('B5'); sheet.setCurrentCell(cell); const selection = sheet.getSelection(); // Current cell: B5 const currentCell = selection.getCurrentCell();
參數
名稱 | 類型 | 說明 |
---|---|---|
cell | Range | 要設為目前儲存格的儲存格。 |
回攻員
Range
:新設定的目前儲存格。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Frozen Columns(columns)
凍結指定欄數。如果為零,則不會凍結任何資料欄。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Freezes the first column sheet.setFrozenColumns(1);
參數
名稱 | 類型 | 說明 |
---|---|---|
columns | Integer | 要凍結的欄數。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Frozen Rows(rows)
凍結指定列數。如果為零,則不會凍結任何資料列。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Freezes the first row sheet.setFrozenRows(1);
參數
名稱 | 類型 | 說明 |
---|---|---|
rows | Integer | 要凍結的列數。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Iterative Calculation Convergence Threshold(minThreshold)
設定疊代計算的最低門檻值。如果連續計算的結果差異小於這個值,系統就會停止疊代計算。這個值不得為負數,預設值為 0.05。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Sets the iterative calculation convergence threshold for the spreadsheet. ss.setIterativeCalculationConvergenceThreshold(2); // Logs the threshold to the console. console.log(ss.getIterativeCalculationConvergenceThreshold());
參數
名稱 | 類型 | 說明 |
---|---|---|
min | Number | 收斂臨界值下限 (不得為負值)。 |
回攻員
Spreadsheet
- 這個試算表,用於串連。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Iterative Calculation Enabled(isEnabled)
設定是否要在這份試算表中啟用反覆運算。如果啟用計算時,尚未設定計算週期上限和收斂門檻,系統會分別預設為 50 和 0.05。如果先前已設定其中一項,則會保留先前的值。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Activates iterative calculation on the spreadsheet. ss.setIterativeCalculationEnabled(true); // Logs whether iterative calculation is activated for the spreadsheet. console.log(ss.isIterativeCalculationEnabled());
參數
名稱 | 類型 | 說明 |
---|---|---|
is | Boolean | 如果應啟用反覆運算,則為 true ;否則為 false 。 |
回攻員
Spreadsheet
- 這個試算表,用於串連。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Max Iterative Calculation Cycles(maxIterations)
設定疊代計算期間應執行的計算疊代次數上限。這個值必須介於 1 到 10,000 (含首尾),預設為 50。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Sets the max iterative calculation cycles for the spreadsheet. ss.setMaxIterativeCalculationCycles(10); // Logs the max iterative calculation cycles to the console. console.log(ss.getMaxIterativeCalculationCycles());
參數
名稱 | 類型 | 說明 |
---|---|---|
max | Integer | 計算疊代次數上限 (介於 1 到 10,000 之間)。 |
回攻員
Spreadsheet
- 這個試算表,用於串連。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Named Range(name, range)
為範圍命名。
// The code below creates a new named range "TaxRates" in the active spreadsheet const ss = SpreadsheetApp.getActiveSpreadsheet(); ss.setNamedRange('TaxRates', SpreadsheetApp.getActiveRange());
參數
名稱 | 類型 | 說明 |
---|---|---|
name | String | 範圍的名稱。 |
range | Range | 範圍規格。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Recalculation Interval(recalculationInterval)
設定試算表重新計算的頻率。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Sets the calculation interval for the spreadsheet to 'ON_CHANGE'. const interval = ss.setRecalculationInterval( SpreadsheetApp.RecalculationInterval.ON_CHANGE, ); // Logs the calculation interval to the console. console.log(interval);
參數
名稱 | 類型 | 說明 |
---|---|---|
recalculation | Recalculation | 新的重新計算間隔。 |
回攻員
Spreadsheet
- 這個試算表,用於串連。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Row Height(rowPosition, height)
以像素為單位,設定指定資料列的高度。根據預設,資料列會配合儲存格內容調整大小。如要強制將資料列設為特定高度,請使用 Sheet.setRowHeightsForced(startRow, numRows, height)
。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Sets the first row to a height of 200 pixels sheet.setRowHeight(1, 200);
參數
名稱 | 類型 | 說明 |
---|---|---|
row | Integer | 要變更的列位置。 |
height | Integer | 要設定的高度 (以像素為單位)。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Spreadsheet Locale(locale)
設定試算表語言代碼。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Sets the spreadsheet locale. ss.setSpreadsheetLocale('fr'); // Gets the spreadsheet locale. const ssLocale = ss.getSpreadsheetLocale(); // Logs the locale to the console. console.log(ssLocale);
參數
名稱 | 類型 | 說明 |
---|---|---|
locale | String | 要使用的語言代碼 (例如「en」、「fr」或「en_US」)。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Spreadsheet Theme(theme)
在試算表上設定主題。
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // The code below sets the second predefined theme as the current theme of the // spreadsheet. const predefinedThemesList = spreadsheet.getPredefinedSpreadsheetThemes(); spreadsheet.setSpreadsheetTheme(predefinedThemesList[1]);
參數
名稱 | 類型 | 說明 |
---|---|---|
theme | Spreadsheet | 要套用的主題。 |
回攻員
Spreadsheet
:新的目前主題。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
set Spreadsheet Time Zone(timezone)
設定試算表的時區。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets spreadsheet, you can use // SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Sets the time zone of the spreadsheet. ss.setSpreadsheetTimeZone('America/New_York'); // Gets the time zone of the spreadsheet. const ssTimeZone = ss.getSpreadsheetTimeZone(); // Logs the time zone to the console. console.log(ssTimeZone);
參數
名稱 | 類型 | 說明 |
---|---|---|
timezone | String | 時區,以「long」格式指定 (例如「America/New_York」,如 Joda.org 所列)。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
show(userInterface)
在使用者瀏覽器檢視區塊中央的對話方塊中,顯示自訂使用者介面元件。伺服器端指令碼的執行作業不會暫停。如要與伺服器端通訊,使用者介面元件必須對伺服器端指令碼進行非同步回呼。
如果伺服器端指令碼先前顯示的對話方塊尚未關閉,系統會以新要求對話方塊的使用者介面取代現有對話方塊。
下列程式碼片段會在對話方塊中顯示簡易的 Html
應用程式,並指定標題、高度和寬度:
const htmlApp = HtmlService .createHtmlOutput( '<p>A change of speed, a change of style...</p>', ) .setTitle('My HtmlService Application') .setWidth(250) .setHeight(300); SpreadsheetApp.getActiveSpreadsheet().show(htmlApp); // The script resumes execution immediately after showing the dialog.
參數
名稱 | 類型 | 說明 |
---|---|---|
user | Object | Html 。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/script.container.ui
sort(columnPosition)
依欄遞增排序工作表。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Sorts the sheet by the first column, ascending sheet.sort(1);
參數
名稱 | 類型 | 說明 |
---|---|---|
column | Integer | 做為排序依據的資料欄。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
sort(columnPosition, ascending)
依資料欄排序工作表。這個函式會採用參數來指定遞增或遞減。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Sorts the sheet by the first column, descending sheet.sort(1, false);
參數
名稱 | 類型 | 說明 |
---|---|---|
column | Integer | 做為排序依據的資料欄。 |
ascending | Boolean | true 代表遞增排序,false 代表遞減排序。 |
回攻員
Sheet
:工作表,適用於方法鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
toast(msg)
在試算表右下角顯示含有指定訊息的彈出式視窗。
// Show a popup with the message "Task started". SpreadsheetApp.getActiveSpreadsheet().toast('Task started');
參數
名稱 | 類型 | 說明 |
---|---|---|
msg | String | 要在祝賀訊息中顯示的訊息。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
toast(msg, title)
在試算表的右下角顯示彈出式視窗,並顯示指定訊息和標題。
// Show a popup with the title "Status" and the message "Task started". SpreadsheetApp.getActiveSpreadsheet().toast('Task started', 'Status');
參數
名稱 | 類型 | 說明 |
---|---|---|
msg | String | 要在祝賀訊息中顯示的訊息。 |
title | String | 祝賀訊息的選填標題。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
toast(msg, title, timeoutSeconds)
在試算表的右下角顯示彈出式視窗,其中包含指定的標題和訊息,並在一段時間後消失。
// Show a 3-second popup with the title "Status" and the message "Task started". SpreadsheetApp.getActiveSpreadsheet().toast('Task started', 'Status', 3);
參數
名稱 | 類型 | 說明 |
---|---|---|
msg | String | 要在祝賀訊息中顯示的訊息。 |
title | String | 祝賀訊息的選填標題。 |
timeout | Number | 逾時時間 (以秒為單位);如果為 null ,則預設為 5 秒; 如果為負值,則會顯示訊息,直到使用者關閉為止。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
unhide Column(column)
取消隱藏指定範圍內的資料欄。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // This unhides the first column if it was previously hidden const range = sheet.getRange('A1'); sheet.unhideColumn(range);
參數
名稱 | 類型 | 說明 |
---|---|---|
column | Range | 要取消隱藏的範圍 (如有隱藏)。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
unhide Row(row)
取消隱藏指定範圍內的資料列。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // This unhides the first row if it was previously hidden const range = sheet.getRange('A1'); sheet.unhideRow(range);
參數
名稱 | 類型 | 說明 |
---|---|---|
row | Range | 要取消隱藏的範圍 (如有隱藏)。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
update Menu(name, subMenus)
更新由 add
新增的選單。運作方式與 add
完全相同。
const ss = SpreadsheetApp.getActiveSpreadsheet(); const menuEntries = []; menuEntries.push({name: 'Lone Menu Entry', functionName: 'function1'}); ss.updateMenu('addMenuExample', menuEntries);
參數
名稱 | 類型 | 說明 |
---|---|---|
name | String | 要更新的菜單名稱。 |
sub | Object[] | 含有 name 和 function 參數的 JavaScript 地圖陣列。您可以使用內含程式庫中的函式,例如 Library.libFunction1 。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
wait For All Data Executions Completion(timeoutInSeconds)
等待試算表中的所有目前執行作業完成,並在指定秒數後逾時。如果執行作業在逾時時未完成,系統會擲回例外狀況,但不會取消資料執行作業。
參數
名稱 | 類型 | 說明 |
---|---|---|
timeout | Integer | 等待資料執行作業的時間 (以秒為單位)。最多 300 秒。 |
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets