Skip to content

Instantly share code, notes, and snippets.

View mark05e's full-sized avatar

mark05E mark05e

View GitHub Profile
/**
* Writes fileContents to a file with the given fileName in the specified folder. If a file with the
* same name already exists, it will either overwrite it or append to it, based on the value of the append parameter.
*
* @param {string} fileName - The name of the file to write to.
* @param {string} fileContents - The contents to write to the file.
* @param {string} folderIdOrUrl - The ID or URL of the folder to write the file to.
* @param {boolean} [append=false] - Whether to append the fileContents to an existing file with the same name.
* @returns {string} The ID of the created or modified file.
*/
/**
* Retrieves the filenames and corresponding file IDs of all files within a given Google Drive folder.
*
* @param {string} folderId - The ID of the folder.
* @returns {Object[]} - An array of objects containing the file ID and name.
*/
function getFilenamesAndIds(folderId) {
// Get the folder using the provided folderId
let folder = DriveApp.getFolderById(folderId);
/**
* Logs information to a Google Spreadsheet for tracking and debugging purposes.
*
* @param {string} functionName - The name of the function being logged.
* @param {string} functionState - The state or status of the function being logged.
* @param {string|object} message - The log message to be recorded, which can be a string or an object.
* @param {string} spreadsheetUrl - The URL of the logging spreadsheet.
*/
function logToSpreadsheet(functionName, functionState, message, spreadsheetUrl) {
// Get the active sheet of the Google Spreadsheet
/**
* Replaces the domain of a URL with the domain of another URL.
*
* @param {string} url1 - The first URL containing the target domain to be replaced.
* @param {string} url2 - The second URL containing the replacement domain.
* @returns {string} - The modified URL with the domain replaced.
*/
function replaceDomain(url1, url2) {
// Regular expression pattern to match the domain of a URL
let urlRegex = /https:\/\/[^/]+/;
/**
* Extracts the ID from a Google Docs URL.
*
* @param {string} url - The Google Docs URL.
* @returns {string|null} - The extracted ID from the URL, or null if no ID is found.
*/
function getIdFromUrl(url) {
// Use a regular expression to match and extract the ID from the URL
let result = url.match(/[-\w]{25,}(?!.*[-\w]{25,})/);
/**
* Checks if a given input is a valid JSON string or an object.
*
* @param {string|object} input - The input to be checked, which can be a JSON string or an object.
* @returns {boolean} - Returns true if the input is a valid JSON string or an object, false otherwise.
*/
function isJSON(input) {
// Check if the input is not a string
if (typeof input !== 'string') {
// Check if the input is already an object
/**
* Checks if a given string contains any space character.
*
* @param {string} str - The input string to be checked.
* @throws {Error} Throws an error if the string contains a space.
* @returns {boolean} - Returns true if the string does not contain a space.
*/
function checkForSpace(str) {
// Check if the string includes a space character
if (str.includes(' ')) {
/**
* Returns the first five characters followed by an ellipsis (...) and the last five characters of a given string.
* If the string length is less than or equal to 10, the original string is returned.
*
* @param {string} str - The input string.
* @returns {string} - The modified string with the first and last five characters, separated by an ellipsis.
*/
function showFirstAndLastFive(str) {
// Check if the string length is less than or equal to 10
if (str.length <= 10) {
@mark05e
mark05e / callLibraryFunction.gs
Last active December 4, 2022 23:41
Based on function documented on [](https://stackoverflow.com/questions/48928932/call-library-function-from-html-with-google-script-run) with some enhancements for my usecase.
// Function - callLibraryFunction
// based on function documented on
// https://stackoverflow.com/questions/48928932/call-library-function-from-html-with-google-script-run
// with some enhancements for my usecase.
//
function callLibraryFunction(functionPathAndName, ...args){
let fnPathArray = functionPathAndName.split(".");
let fnPathLength = fnPathArray.length
let libFunc = fnPathArray[fnPathArray.length - 1];
# ██████╗ ███████╗███╗ ███╗ ██████╗ ██╗ ██╗███████╗ ██╗ ██╗██████╗
# ██╔══██╗██╔════╝████╗ ████║██╔═══██╗██║ ██║██╔════╝ ██║ ██║██╔══██╗
# ██████╔╝█████╗ ██╔████╔██║██║ ██║██║ ██║█████╗ ███████║██████╔╝
# ██╔══██╗██╔══╝ ██║╚██╔╝██║██║ ██║╚██╗ ██╔╝██╔══╝ ██╔══██║██╔═══╝
# ██║ ██║███████╗██║ ╚═╝ ██║╚██████╔╝ ╚████╔╝ ███████╗ ██║ ██║██║
# ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═══╝ ╚══════╝ ╚═╝ ╚═╝╚═╝
#
# ██████╗ ██╗ ██████╗ █████╗ ████████╗██╗ ██╗ █████╗ ██████╗ ███████╗
# ██╔══██╗██║ ██╔═══██╗██╔══██╗╚══██╔══╝██║ ██║██╔══██╗██╔══██╗██╔════╝
# ██████╔╝██║ ██║ ██║███████║ ██║ ██║ █╗ ██║███████║██████╔╝█████╗