Skip to content

Instantly share code, notes, and snippets.

@japboy
Last active February 9, 2019 03:06
Show Gist options
  • Save japboy/4ef3d0d2188d7763117838720146107c to your computer and use it in GitHub Desktop.
Save japboy/4ef3d0d2188d7763117838720146107c to your computer and use it in GitHub Desktop.
google app scripts
var SLACK_WEBHOOK_URL = 'https://hooks.slack.com/services/XXXXXXXXXX';
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [
{
name: 'Notify to Slack',
functionName: 'notify',
},
];
spreadsheet.addMenu('Notify', menuEntries);
}
function notify() {
var payload = {
channel: '#target_channel',
username: 'whoami',
icon_url: 'https://via.placeholder.com/128',
text: 'Hello @whoami',
link_names: 1,
attachments: [
{
title: 'tl;dr',
text: 'bla bla bla.',
},
],
};
var response = requestToSlack_(payload);
}
function requestToSlack_(payload){
var url = SLACK_WEBHOOK_URL;
var options = {
method: 'post',
contentType : 'application/json',
payload: JSON.stringify(payload),
};
var response = UrlFetchApp.fetch(url, options);
return response;
}
var TARGET_SHEET = 'Sheet1';
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [
{
name: 'Debug cell',
functionName: 'debug',
},
];
spreadsheet.addMenu('Debug', menuEntries);
}
function debug() {
var cell = getCell_(rows, function (rowIndex, columnIndex, cell) {
return cell === true;
});
console.log(cell);
}
function getCell_(rows, predicate) {
for (var i = 0; i < rows.length; i += 1) {
var columns = rows[i]
for (var j = 0; j < columns.length; j += 1) {
var cell = columns[j];
if (predicate(i, j, cell)) {
return {
rowIndex: i + 1,
columnIndex: j + 1,
cell: cell,
};
}
}
}
throw new Error('Target cell not found.');
}
function getRows_(sheet, minRows, minColumns, maxRows, maxColumns) {
var actualMinRows = minRows || 1;
var actualMinColumns = minColumns || 1;
var actualMaxRows = maxRows || sheet.getMaxRows();
var actualMaxColumns = maxColumns || sheet.getMaxColumns();
var dataRange = sheet.getRange(actualMinRows, actualMinColumns, actualMaxRows, actualMaxColumns);
return dataRange.getValues();
}
function getSheet_(name) {
var sheets = SpreadsheetApp.getActive().getSheets();
for (var i = 0; i < sheets.length; i += 1) {
if (name === sheets[i].getName()) {
return sheets[i];
}
}
throw new Error('Target sheet not found.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment