Created
June 22, 2018 17:04
-
-
Save Leocardoso94/706b007122b63c186f236bae840913b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var _extends = | |
| Object.assign || | |
| function (target) { | |
| for (var i = 1; i < arguments.length; i++) { | |
| var source = arguments[i]; | |
| for (var key in source) { | |
| if (Object.prototype.hasOwnProperty.call(source, key)) { | |
| target[key] = source[key]; | |
| } | |
| } | |
| } | |
| return target; | |
| }; | |
| function _defineProperty(obj, key, value) { | |
| if (key in obj) { | |
| Object.defineProperty(obj, key, { | |
| value: value, | |
| enumerable: true, | |
| configurable: true, | |
| writable: true | |
| }); | |
| } else { | |
| obj[key] = value; | |
| } | |
| return obj; | |
| } | |
| var arrayToAnObject = function arrayToAnObject(prev, current) { | |
| return _extends({}, prev, current); | |
| }; | |
| var normalizeSheetsData = function normalizeSheetsData() { | |
| var sheetsData = | |
| arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | |
| var keys = | |
| arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | |
| return sheetsData.map(function (values) { | |
| return values | |
| .map(function (value, index) { | |
| return _defineProperty({}, keys[index], value); | |
| }) | |
| .reduce(arrayToAnObject, {}); | |
| }); | |
| }; | |
| function slugify(str) { | |
| str = str || ""; | |
| var a = | |
| "àáäâèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;άαβγδεέζήηθιίϊΐκλμνξοόπρσςτυϋύΰφχψωώ"; | |
| var b = | |
| "aaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh------aavgdeeziitiiiiklmnxooprsstyyyyfhpoo"; | |
| var p = new RegExp(a.split("").join("|"), "g"); | |
| return str | |
| .toString() | |
| .trim() | |
| .toLowerCase() | |
| .replace(/ου/g, "ou") | |
| .replace(/ευ/g, "eu") | |
| .replace(/θ/g, "th") | |
| .replace(/ψ/g, "ps") | |
| .replace(/\//g, "-") | |
| .replace(/\s+/g, "-") // Replace spaces with - | |
| .replace(p, function (c) { | |
| return b.charAt(a.indexOf(c)); | |
| }) // Replace special chars | |
| .replace(/&/g, "-e-") // Replace & with 'and' | |
| .replace(/[^\w\-]+/g, "") // Remove all non-word chars | |
| .replace(/\-\-+/g, "-") // Replace multiple - with single - | |
| .replace(/^-+/, "") // Trim - from start of text | |
| .replace(/-+$/, ""); // Trim - from end of text | |
| } | |
| var isSomeCaseWithTheSameTitle = function isSomeCaseWithTheSameTitle(kase, title) { | |
| return slugify(kase.title) === slugify(title); | |
| }; | |
| var generateSlug = function generateSlug(title, cases, sufix) { | |
| sufix = sufix || 1; | |
| var caseWithTheSameTitles = cases.filter(function (kase) { | |
| return isSomeCaseWithTheSameTitle(kase, title); | |
| }); | |
| return caseWithTheSameTitles.length > 0 ? slugify(title + '-' + caseWithTheSameTitles.length) : slugify(title); | |
| }; | |
| function myFunction() { | |
| var start = new Date(); | |
| var COLUMN_SLUG = 4; | |
| var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
| var worksheet = spreadsheet.getSheetByName("Página6"); | |
| var rows = worksheet.getDataRange().getNumRows(); | |
| var values = worksheet.getSheetValues(1, 3, rows + 1, 2); | |
| var keys = values.shift(); | |
| var cases = normalizeSheetsData(values, keys); | |
| cases.forEach(function (kase, row) { | |
| var slug = ("" + kase.title).length > 0 ? generateSlug(kase.title, cases.slice(0, row)) : ''; | |
| var ACTUAL_LINE = row + 2; | |
| worksheet.getRange(ACTUAL_LINE, COLUMN_SLUG).setValue(slug); | |
| }); | |
| var end = new Date(); | |
| Logger.log('Duration: %sms', end - start); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment