iTranslated by AI
A Collection of Methods for Manipulating Google Sheets with GAS
Introduction
Hello everyone,
This is Ookuma.
In this article, I have compiled various methods for manipulating Google Sheets using GAS (Google Apps Script) under the title "Miscellaneous Methods for Operating Sheets with GAS."
I hope this proves useful to you.
Target Audience
Note
What is GAS?
GAS stands for Google Apps Script, a scripting platform developed by Google.
It is utilized as a tool for developing applications to automate and integrate various services provided by Google.
The scripting language used in GAS is based on JavaScript.
Since the basic syntax is the same as JavaScript, those who have used JavaScript before should find it relatively easy to master.
At our company, we also use GAS to automate and streamline various business tasks.
Methods for Operating Sheets
Getting a Spreadsheet
function getSpreadsheet() {
return SpreadsheetApp.getActiveSpreadsheet();
}
function getSpreadsheetById(id) {
return SpreadsheetApp.openById(id);
}
function getSpreadsheetByUrl(url) {
return SpreadsheetApp.openByUrl(url);
}
Getting a Sheet
function getActiveSheet() {
return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
}
function getSheetByName(sheetName) {
return SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
}
function getSheetByNameOrCreate(sheetName) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
if (!sheet) {
sheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet(sheetName);
}
return sheet;
}
function getSheetsByNameContains(sheetName) {
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
return sheets.filter(function(sheet) {
return sheet.getName().indexOf(sheetName) !== -1;
});
}
function getSheetByIndex(index) {
return SpreadsheetApp.getActiveSpreadsheet().getSheets()[index];
}
Adding a Sheet
function addSheet() {
return SpreadsheetApp.getActiveSpreadsheet().insertSheet();
}
function addSheet(sheetName) {
return SpreadsheetApp.getActiveSpreadsheet().insertSheet(sheetName);
}
function addSheetWithIndex(index) {
return SpreadsheetApp.getActiveSpreadsheet().insertSheet(index);
}
Deleting a Sheet
function deleteSheet() {
return SpreadsheetApp.getActiveSpreadsheet().deleteActiveSheet();
}
function deleteSheet(sheetName) {
return SpreadsheetApp.getActiveSpreadsheet().deleteSheet(getSheetByName(sheetName));
}
function getSheetByName(sheetName) {
return SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
}
function deleteSheetByIndex(index) {
return SpreadsheetApp.getActiveSpreadsheet().deleteSheet(getSheetByIndex(index));
}
function getSheetByIndex(index) {
return SpreadsheetApp.getActiveSpreadsheet().getSheets()[index];
}
Copying a Sheet
function copySheet() {
return SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();
}
function copySheetByName(sheetName, newSheetName) {
var sheet = getSheetByName(sheetName);
var newSheet = sheet.copyTo(SpreadsheetApp.getActiveSpreadsheet());
newSheet.setName(newSheetName);
return newSheet;
}
function getSheetByName(sheetName) {
return SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
}
function copySheetByIndex(index, newSheetName) {
var sheet = getSheetByIndex(index);
var newSheet = sheet.copyTo(SpreadsheetApp.getActiveSpreadsheet());
newSheet.setName(newSheetName);
return newSheet;
}
function getSheetByIndex(index) {
return SpreadsheetApp.getActiveSpreadsheet().getSheets()[index];
}
Sorting Sheets
function sortSheetsByNameAsc() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheets = spreadsheet.getSheets();
sheets.sort(function(a, b) {
return a.getName().localeCompare(b.getName());
});
for (var i = 0; i < sheets.length; i++) {
spreadsheet.setActiveSheet(sheets[i]);
spreadsheet.moveActiveSheet(i + 1);
}
}
function sortSheetsByNameDesc() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheets = spreadsheet.getSheets();
sheets.sort(function(a, b) {
return b.getName().localeCompare(a.getName());
});
for (var i = 0; i < sheets.length; i++) {
spreadsheet.setActiveSheet(sheets[i]);
spreadsheet.moveActiveSheet(i + 1);
}
}
Entering Values into Cells
function setCellValueInActiveSheet(cellAddress, value) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getRange(cellAddress).setValue(value);
}
Setting Hyperlinks in Cells
function setHyperlinkInActiveSheet(cellAddress, url, label) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getRange(cellAddress).setFormula('=HYPERLINK("' + url + '","' + label + '")');
}
Getting Values from Cells
function getCellValueInActiveSheet(cellAddress) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
return sheet.getRange(cellAddress).getValue();
}
Clearing Cell Values
function clearCellValueInActiveSheet(cellAddress) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getRange(cellAddress).clearContent();
}
Copying Cell Values
function copyCellValueInActiveSheet(fromCellAddress, toCellAddress) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var value = sheet.getRange(fromCellAddress).getValue();
sheet.getRange(toCellAddress).setValue(value);
}
Merging Cells
function mergeCellsInActiveSheet(startRow, startColumn, numRows, numColumns) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getRange(startRow, startColumn, numRows, numColumns).merge();
}
Setting Cell Background Color
function setBackgroundColorInActiveSheet(cellAddress, color) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getRange(cellAddress).setBackground(color);
}
Setting Cell Font Color
function setFontColorInActiveSheet(cellAddress, color) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getRange(cellAddress).setFontColor(color);
}
Setting Cell Font Size
function setFontSizeInActiveSheet(cellAddress, fontSize) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getRange(cellAddress).setFontSize(fontSize);
}
Conclusion
In this article, I have compiled various methods for operating Google Sheets using GAS.
For example, when writing data obtained via API to Google Sheets, using the methods above allows for efficient processing.
Please feel free to use them.
Thank you for reading until the end.
References
Discussion