Skip to content

Instantly share code, notes, and snippets.

View Oaphi's full-sized avatar
💭
☕ and 👩‍💻

Oleg Valter Oaphi

💭
☕ and 👩‍💻
  • Rocket
  • Saint Petersburg
View GitHub Profile
@Oaphi
Oaphi / asyncGAPI.js
Last active February 24, 2021 15:27
Async-friendly google.script.run utility
/**
* Promise-friendly google.script.run call
* @param {String} funcName
* @param {...*} params
* @returns {Promise}
*/
const asyncGAPI = (funcName, ...params) => {
return new Promise((res, rej) => {
google.script.run
.withSuccessHandler(data => res(data))
@Oaphi
Oaphi / WebAppSelfHelp.md
Last active November 22, 2022 18:48
Web App troubleshooting questionnaire

Preliminary

  1. Does the Web App have a saved deployment? ("Publish" -> "Deploy as Web App")
  2. Are you accessing the /dev endpoint (https://script.google.com/.../yourIdHere/dev)?
  3. If you are accessing the /exec endpoint (https://script.google.com/.../yourIdHere/exec), did you redeploy?
  4. If you redeployed, did you increment the script version?
  5. If you have a client-side problem, what does the console say (pres F12 to open developer tools)?
  6. If you have a server-side problem, what do executions say (https://script.google.com/home/projects/yourIdHere/executions

Web App Access

@Oaphi
Oaphi / PropertyLockService.js
Created August 7, 2020 13:06
PropertyLockService
var PropertyLock = (() => {
let locked = false;
let timeout = 0;
const store = PropertiesService.getScriptProperties();
const propertyName = "locked";
const triggerName = "PropertyLock.releaseLock";
@Oaphi
Oaphi / withInterval.js
Created December 3, 2020 21:05
Promise-based interval runner (cancellable)
const withInterval = async ({
timeouts = [],
delay = 0,
interval = 4,
callback,
times = 1,
stopIf = () => false
}) => {
if (!times) {
return;
@Oaphi
Oaphi / gas-unicode-hex.ts
Last active December 6, 2020 05:54
Convert Unicode literal to hexadecimal byte sequence
/**
* @summary converts unicode literal \u{<code point>} into hex byte sequence \x<byte>...
*/
const fromUnicodeToHexBytes = (unicode: string) => {
//make bytes unsigned -128 +127 -> +256 or & 0xff -> 0 256
return Utilities.newBlob(unicode)
.getBytes()
.map((bt) => `\\x${(bt & 0xff).toString(16)}`)
.join("");
};
@Oaphi
Oaphi / DriveUtils.ts
Created December 7, 2020 01:33
Drive utilities
const getFolderByName = (name: string, create = false) => {
const iter = DriveApp.getFoldersByName(name);
return iter.hasNext()
? iter.next()
: create
? DriveApp.createFolder(name)
: null;
};
@Oaphi
Oaphi / WidthMeasure.js
Last active December 7, 2020 04:25
Preciser text width measure (single line, obviously needs transpiling for the browser detection to work)
/**
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/TextMetrics}
* @summary measure text width
* @param {CanvasRenderingContext2D} ctx drawing context
* @returns {number}
*/
const measureWidth = (ctx, text) => {
const measurement = ctx.measureText(text);
@Oaphi
Oaphi / webpack-setup.md
Created February 5, 2021 04:05
Webpack installation notes

Quick install for HTML-based projects:

npm install --save-dev 
  webpack \
  clean-webpack-plugin \
  css-minimizer-webpack-plugin \
  html-webpack-plugin \
  html-loader
@Oaphi
Oaphi / recursiveAppend.ts
Created February 7, 2021 20:44
Recursive append Node.js utility
import { BaseEncodingOptions } from "fs";
import { appendFile, mkdir } from "fs/promises";
import { parse, sep } from "path";
const recursiveAppend = async (
path: string,
encoding: BaseEncodingOptions["encoding"] = "utf-8"
) => {
const parts = path.split(sep);
@Oaphi
Oaphi / HTMLSelectElementUtils.js
Last active February 9, 2021 00:52
Useful utilities for HTMLSelectElement
/**
* @summary extracts values from select
* @param {HTMLSelectElement} sel
* @return {string[]}
*/
const getSelectVals = ({ options }) =>
Array.from(options).map(({ value }) => value);
/**
* @summary checks if select has a value