Skip to content

Instantly share code, notes, and snippets.

@oshliaer
Last active May 24, 2018 13:36
Show Gist options
  • Save oshliaer/c47c63ed943b09450f2ec64550411ded to your computer and use it in GitHub Desktop.
Save oshliaer/c47c63ed943b09450f2ec64550411ded to your computer and use it in GitHub Desktop.
The folder creator

Folder Creator

Создает папку с заданными параметрами из активной строки текущей Таблицы

Настройка

var POSITIONS = {
  name: 1, //Число - номер колонки, в которой хранится имя новой папки. Не число - будет добавлено это значение.
  parent: 'root',  //Число - номер колонки, в которой хранится id родительской папки. Не число - будет выбран root.
  destination: 5, //Колонка, куда будет добавлен результат работы
}

Запуск

  • Скопировать и вставить в скрипт, привязанный к Таблице
  • ИЛИ скопировать пример
  • В меню запустить Folder Creator - Action
var POSITIONS = {
name: 1,
parent: 'root',
destination: 5
}
function onOpen(){
SpreadsheetApp.getUi().createMenu('Folder Creator').addItem('Action', 'action').addToUi();
}
function createFolder_(settings) {
return settings.parent.createFolder(settings.name);
}
function pastData(data){
return SpreadsheetApp.getActiveSheet().getRange(data.row, data.column).setValue(data.value);
}
function action(){
var activeRow = SpreadsheetApp.getActiveRange().getRow();
var activeData = SpreadsheetApp.getActiveSheet().getRange([activeRow, activeRow].join(':')).getValues()[0];
var positions = {
name : isReal(POSITIONS.name) ? activeData[POSITIONS.name - 1] : POSITIONS.name
};
if(isReal(POSITIONS.parent)){
positions.parent = DriveApp.getFolderById(activeData[POSITIONS.parent - 1]);
};
if(!positions.parent) positions.parent = DriveApp.getRootFolder();
var res = createFolder_(positions);
SpreadsheetApp.getActiveSpreadsheet().toast(Utilities.formatString('The script has created the folder "%s"', res.getName()));
if(!isReal(POSITIONS.destination)) return;
var res2 = pastData({
row: activeRow,
column: POSITIONS.destination,
value: Utilities.formatString('=HYPERLINK("https://drive.google.com/drive/folders/%s";"%s")', res.getId(), res.getName())
});
}
function isReal(n) {
return n % 1 === 0 && n > 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment