Skip to content

Instantly share code, notes, and snippets.

@tuyendq
Last active May 19, 2025 16:09
Show Gist options
  • Select an option

  • Save tuyendq/c2dac35e0f5753d6a1b5077ee5ae4de7 to your computer and use it in GitHub Desktop.

Select an option

Save tuyendq/c2dac35e0f5753d6a1b5077ee5ae4de7 to your computer and use it in GitHub Desktop.
Google Sheets commonly used functions
/**
* Insert current date (today) into current cell
*/
function InsertToday() {
var currentDate = Utilities.formatDate(new Date(), 'Asia/Ho_Chi_Minh', 'dd/MM/YYYY');
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getCurrentCell().setValue(currentDate);
};
/**
* Delete columns and rows
*/
function DeleteColumnsAndRows() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName("SHEET-NAME-HERE");
// sheet.getRange(1, 2, sheet.getMaxRows(), sheet.getMaxRows()).activate();
// Delete column B to max
if (sheet.getMaxColumns() > 1) {
sheet.deleteColumns(2, sheet.getMaxColumns()-1);
}
// Delete row 2 to max
if (sheet.getMaxRows() > 1) {
sheet.deleteRows(2, sheet.getMaxRows()-1);
}
};
// Get value from "Price" sheet, 'N2' cell
var cellValue = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Price").getRange('N2').getValue();
// Logger.log(cellValue);
/**
* Duplicate current row [insert above]
*/
function CopyRowInsertAbove() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
sheet.insertRowsBefore(spreadsheet.getActiveRange().getRow(), 1);
spreadsheet.getActiveRange().offset(0, 1 - spreadsheet.getCurrentCell().getColumn(), 1, spreadsheet.getActiveRange().getNumColumns()).activate()
sheet.getRange(spreadsheet.getCurrentCell().getRow() + 1, 1, 1, sheet.getMaxColumns()).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};
@tuyendq

tuyendq commented May 15, 2025

Copy link
Copy Markdown
Author

Add function CopyRowInsertAbove()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment