Skip to content

Instantly share code, notes, and snippets.

@mark05e
Last active May 21, 2022 15:37
Show Gist options
  • Save mark05e/d1377d737681c776b50e8575d019299c to your computer and use it in GitHub Desktop.
Save mark05e/d1377d737681c776b50e8575d019299c to your computer and use it in GitHub Desktop.
// https://github.com/choraria/gas-url-shortener/blob/master/apps-script/Helper.gs
function removeEmptyColumns(sheetName) {
var activeSheet = ss.getSheetByName(sheetName)
var maxColumns = activeSheet.getMaxColumns();
var lastColumn = activeSheet.getLastColumn();
if (maxColumns-lastColumn != 0){
activeSheet.deleteColumns(lastColumn+1, maxColumns-lastColumn);
}
}
function makeHeadersBold(sheetName) {
var activeSheet = ss.getSheetByName(sheetName);
var header = activeSheet.getRange(1, 1, 1, activeSheet.getLastColumn()).setFontWeight("bold");
}
function getCustomDomain() {
return customDomain;
}
// https://gist.github.com/demoive/8d08fd206b8025c301165a4bec78e5f6
/**
* Returns an ENUM string meant to represent the "type" of edit which occured:
*
* - MULTIPLE_CELLS: Multiple cells were edited simultaneously.
* - PASTE_OR_UNDO: The single cell edited was from a "paste" or "undo" action.
* - BLANK_OLD: The single cell edited was previously blank.
* - BLANK_NEW: The single cell edited was cleared out.
* - STANDARD: A "regular" edit, usually meaning a single cell change from one value to another.
*
* We infer the type of edit by checking 1 of 3 possible values of the `oldValue` and `value`
* properties on original event object, all combinations of which are provided in the table below:
*
* +-------------------+------------+-----------+----------------+
* | Example | e.oldValue | e.value | Name |
* +-------------------+------------+-----------+----------------+
* | "test1" > "test2" | "test1" | "test2" | STANDARD |
* | (blank) > "test2" | -- | "test2" | BLANK_OLD |
* | "test1" > (blank) | "test1" | -- | BLANK_NEW |
* | [paste] | -- | -- | PASTE_OR_UNDO |
* | [undo] | -- | -- | PASTE_OR_UNDO |
* | [multiple range] | -- | -- | MULTIPLE_CELLS |
* +-------------------+------------+-----------+----------------+
*
* Documentation source: https://developers.google.com/apps-script/guides/triggers/events#edit
*/
function getEditTypeFromEventObject(editEvent) {
const editEventProps = Object.keys(editEvent);
var editType = "STANDARD";
if (editEvent.range.rowStart !== editEvent.range.rowEnd || editEvent.range.columnStart !== editEvent.range.columnEnd) {
editType = "MULTIPLE_CELLS";
} else if (!editEventProps.includes("value") && !editEventProps.includes("oldValue")) {
editType = "PASTE_OR_UNDO";
} else if (!editEventProps.includes("oldValue") && editEvent.value !== undefined) {
editType = "BLANK_OLD";
} else if (!editEventProps.includes("value") && editEvent.oldValue !== undefined) {
editType = "BLANK_NEW";
}
return editType;
}
/**
* Returns the value of a cell under a column with title {colTitle}
* on the currently active row number. Empty string if cell isn't found.
*
* This is a pass-through function to `getCellValueByColumnTitleOfRowNumber()`
* after the active row index is found.
*
* @param {string} colTitle
* @return {string}
*/
function getCellValueByColumnTitleOfActiveRow(colTitle) {
var activeSheet = SpreadsheetApp.getActiveSheet();
var currentCell = activeSheet.getCurrentCell();
var currentRowNumber = currentCell.getRow();
return getCellValueByColumnTitleOfRowNumber(colTitle, currentRowNumber);
}
/**
* Returns the value of a cell under a column with title {colTitle}
* on row number {rowNumber}. Empty string if cell isn't found.
* Optionally specify the name of the respective sheet as {sheetName}.
*
* @param {string} colTitle
* @param {integer} rowNumber
* @param {string} sheetName
* @return {string}
*/
function getCellValueByColumnTitleOfRowNumber(colTitle, rowNumber, sheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = sheetName
? ss.getSheetByName(sheetName)
: ss.getActiveSheet();
var rowRange = dataSheet.getRange(rowNumber+':'+rowNumber);
var colIndex = getColumnIndexByHeaderTitle(colTitle);
return colIndex !== -1
? rowRange.getValues()[0][colIndex]
: '';
}
/**
* Returns the numbers of rows which have active cells (i.e. selected rows).
*
* @return {Array} An array of integers or an empty array.
*/
function getActiveRowNumbers() {
var activeSheet = SpreadsheetApp.getActiveSheet();
var selection = activeSheet.getSelection();
var currentCell = selection.getCurrentCell();
var ranges = selection.getActiveRangeList().getRanges();
var selectedRows = [];
for (var i = 0; i < ranges.length; i++) {
var rowNumber = ranges[i].getRow();
selectedRows.push(rowNumber);
}
return selectedRows;
}
/**
* Returns the value of a cell under a column with title {colTitle}
* on row number {rowNumber}. Empty string if cell isn't found.
* Optionally specify the name of the respective sheet as {sheetName}.
*
* @param {string} colTitle
* @param {integer} rowNumber
* @param {string} sheetName
* @return {string}
*/
function getCellValueByColumnTitleOfRowNumber(colTitle, rowNumber, sheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = sheetName
? ss.getSheetByName(sheetName)
: ss.getActiveSheet();
var rowRange = dataSheet.getRange(rowNumber+':'+rowNumber);
var colIndex = getColumnIndexByHeaderTitle(colTitle);
return colIndex !== -1
? rowRange.getValues()[0][colIndex]
: '';
}
// https://gist.github.com/ciacicode/94c36a7ff236d1dc4c4e44c150356185
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment