Google Apps Script Code, My Commonly Used


Some of my commonly used Google Apps Script code:

function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu("OnTheMenuBar")
    .addItem("The item on the drop down", "CallTheHTMLfile")
    .addToUi();
}  
  
function CallTheHTMLfile() {
  let sidebar =  HtmlService.createHtmlOutputFromFile("index");
  SpreadsheetApp.getUi().showSidebar(sidebar.setTitle("What shows on top of the sidebar"));
}

Global Variables:

By itself out of any function at the top of the file.

const TMPLTID = "1134567uUoYyTrtEWetc"
const NAMECOL = 13; // Name column 
const TMSTMPCOL = 14; // Timestamp column

Count Number of Populated Column Headers:

“ssURL” passed from the client side.

function addColumnsTrack(ssUrl) {
  let ss = SpreadsheetApp.openByUrl(ssUrl);
  let sheet0 = ss.getSheets()[0];
  let lastColumn = sheet0.getLastColumn();
  let sheet1 = ss.getSheetByName("This Sheet");
  sheet1.insertColumnsAfter(TMSTMPCOL, lastColumn - 1);
}

Copy a Sheet from Another Spreadsheet:

“ssURL” passed from the client side.

function copySheet(ssUrl) {
  let source = SpreadsheetApp.openById(TMPLTID);
  let ssheet = source.getSheets()[0];
  let destination = SpreadsheetApp.openByUrl(ssUrl);
  ssheet.copyTo(destination);
}

Change the Name of a Sheet:

“ssURL” passed from the client side.

function renamePrepForTrack(ssUrl) {
  let ss = SpreadsheetApp.openByUrl(ssUrl);
  let sheet1 = ss.getSheetByName ("Dont Want this Name");
  sheet1.setName("I want this new name");
}

Remove Extra Rows and Filters:

function clearTheSheet() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet();
  const filter = sheet.getFilter();
  const range = sheet.getDataRange();
  const lastRow = range.getLastRow();
  const clrRange = sheet.getRange(2, 1, lastRow, 4);
  if (filter != null) {
    filter.remove()
  }
  clrRange.clear();
  if (lastRow > 2) {
    sheet.deleteRows(2, lastRow - 1);
  }
}

Add Date Conditional Formatting:

function cfDate30(){
  var sheet = SpreadsheetApp.getActiveSheet();
  const MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
  const now = new Date();
  const back30 = new Date(now.getTime() - (MILLIS_PER_DAY * 30));
  const range = sheet.getRange("B2:C");
  const rule = SpreadsheetApp.newConditionalFormatRule()
      .whenDateAfter(back30)
      .setBackground("#DAF7A6")
      .setRanges([range])
      .build();
  const rules = sheet.getConditionalFormatRules();
  rules.push(rule);
  sheet.setConditionalFormatRules(rules);
}

Add Text String Conditional Formatting:

function cfText(){
  const sheet = SpreadsheetApp.getActiveSheet();
  const range = sheet.getRange("A2:A");
  const rule = SpreadsheetApp.newConditionalFormatRule()
      .whenTextContains("Expense")
      .setBackground("#fff2cc")
      .setRanges([range])
      .build();
  const rules = sheet.getConditionalFormatRules();
  rules.push(rule);
  sheet.setConditionalFormatRules(rules);
}

Import CSV Into Sheet:

function importCsv(){
  const ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const id = "1234ghkrUFrDe45678";
  const csv = DriveApp.getFileById(id);
  const blob = csv.getBlob().getDataAsString();
  const blobParse = Utilities.parseCsv(blob);
  const blobLen = blobParse.length;
  const range = ss.getRange(1,2,blobLen,6);
  const rangecb = ss.getRange(2,1,blobLen-1);
  ss.clear();
  range.setValues(blobParse);
  rangecb.insertCheckboxes()
}

Copy Column Headers:

“ssURL” passed from the client side.

function copyColumnHeads(ssUrl) {
  let ss = SpreadsheetApp.openByUrl(ssUrl);
  let sheet0 = ss.getSheets()[0];
  let lastColumn = sheet0.getLastColumn();
  let rangeToCopy = sheet0.getRange(1, 2, 1, lastColumn);
  let sheet1 = ss.getSheetByName("NameOfSheet");
  rangeToCopy.copyTo(sheet1.getRange(1, 9));
}

Delete Contents of Range - Varying Length:

“ssURL” passed from the client side.

function clearFormulas(ssUrl) {
  let ss = SpreadsheetApp.openByUrl(ssUrl);
  let sheet1 = ss.getSheetByName("Name of Sheet");
  let lastRow = sheet1.getLastRow();
  sheet1.getRange(2,9,lastRow).clearContent();
}

Add Formulas to Spreadsheets:

“ssURL” passed from the client side.

function autoFormulas(ssUrl, colNum) {
  let ss = SpreadsheetApp.openByUrl(ssUrl);
  let sheet1 = ss.getSheetByName("Name of Sheet");
  let strtRange = sheet1.getRange(2, 9);
  let lastRow = sheet1.getLastRow();
  strtRange.setFormula("=if(countif('Form Responses 1'!" + colNum + ":" + colNum + ",E2)>0,\"Income\",\"No Sale\")");
  strtRange.copyTo(sheet1.getRange(2, 9, lastRow, 1));
}

Google Apps Script Sidebar Code