2015年11月25日 星期三

ReNamer, Script for delete BEFORE or AFTER specific strings

Some PascalScripts for ReNamer


This Script search and delete filename AFTER the specific strings, include itself.


  1. { This script cut filename by string - }

  2. const
  3.   SEARCH_STRING = 'specific strings here';

  4. var
  5.   BaseName: WideString; Length, Position: Integer;

  6. begin
  7.   BaseName := WideExtractBaseName(FileName);
  8.   Length := WideLength(BaseName);
  9.   Position := WidePos(SEARCH_STRING, BaseName);
  10.   WideDelete(BaseName, Position-1, Length-Position+2);
  11.   FileName := BaseName + WideExtractFileExt(FileName);

  12. end.

This Script opposite to Search and delete filename BEFORE the specific strings, include itself too.


Custom Functions Unique()

Original Function Unique() returns each columns's unique rows in the provided source range.

This script return unique data in provided source range (in each cells) . Rows are returned and sorted.


  1. var cache = CacheService.getUserCache();
  2. function uniqueEach(input) {  
  3.   cache.put("unique", input); // drop input data into cache
  4.   var input = cache.get("unique").toString().split(",").sort(); // receive input as 1D array and sort it
  5.   var result = new Array();
  6.   var cell = input[0]; // give initial value to avoid error
  7.   result.push(cell);
  8.   for (i in input) {
  9.     if (cell != input[i]) {
  10.       cell = input[i];
  11.       result.push(cell);
  12.     }
  13.   }
  14.   return result;
  15. }
Use CacheService to get a simple and fast bunch of data.
And sort make data processing more easy.

Cache Service in Apps Script



Cache Service allows a script to temporarily cache results.

But puts values like Array into Cache, when you take it out will become a String.

Example :
#00#01#02
#10#11#12

Put array into cache when get will return a string.

#00,#01,#02,#10,#11,#12


So we can use Split string, return result to a array object.

#00#01#02#10#11#12

  1. var cache = CacheService.getUserCache();
  2. function putCache(key, array) {
  3.   cache.put(key, array);
  4. }
  5. function getCache(key) {
  6.   var result = cache.get(key).toString().split(",");
  7.   return result;
  8. }