Skip to content

Instantly share code, notes, and snippets.

@jkneko
Created April 6, 2022 00:41
Show Gist options
  • Save jkneko/eba69227f38366dfb5b910baa482dfb8 to your computer and use it in GitHub Desktop.
Save jkneko/eba69227f38366dfb5b910baa482dfb8 to your computer and use it in GitHub Desktop.
[GAS] Copy documents automatically.
// Copyright (c) 2022 jkneko
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Sheet sample (Feel what you can do!)
// https://docs.google.com/spreadsheets/d/1U4eVDZ-a9Wxw6CTnaVJhzdSKl-KaEPYUudNlopsovCY
// Set the main() function to run every morning from 8:00 to 9:00 a.m.
// If you want to enable the Slack posting feature, set the WebHookURL here.
var postUrl = null;
function main(){
const values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
var dayOfWeek = (new Date()).getDay();
var weekHeadIndex = getWeekHead();
values.shift();
values.forEach(function(value){
// If the current day of the week is not checked, do not process
if(value[dayOfWeek + weekHeadIndex] == false){
return;
}
var record = makeRecord(value);
copy(record);
});
}
function getWeekHead()
{
const values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
for(let i=0; i<values[0].length; i++){
Logger.log(values[0][i]);
if(values[0][i] == '日' || values[0][i] == 'Sun')
return i;
}
return -1;
}
function makeRecord(value)
{
var record = {};
record.title = value[0];
record.fromId = value[1];
record.fromName = value[2];
record.fromOffset = value[3];
record.toId = value[4];
record.toName = value[5];
record.toOffset = value[6];
record.channel = value[14];
return record;
}
function getFileFromFolder(folder, name)
{
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
if(file.getName() == name){
return file;
}
}
}
function copy(record) {
try{
var dayFrom = dayjs.dayjs().add(record.fromOffset, 'day');
var fromName = dayFrom.format(record.fromName);
var fromFolder = DriveApp.getFolderById(record.fromId);
var dayTo = dayjs.dayjs().add(record.toOffset, 'day');
var toName = dayTo.format(record.toName);
var toFolder = DriveApp.getFolderById(record.toId);
Logger.log(fromName + ' -> ' + toName);
// Source file to copy
var fromFile = getFileFromFolder(fromFolder, fromName);
// Copy
var toFile = fromFile.makeCopy(toName, toFolder);
// Notify via slack
postToSlack(`${record.title}: ${fromName} is duplicated to ${toName}.\n ${toFile.getUrl()}`, record.channel);
}
catch(e){
return;
}
}
function postToSlack(message, channel) {
if(postUrl == null)
return;
var jsonData =
{
"username" : "Copy documents automatically",
"text" : message,
"channel" : channel
};
var payload = JSON.stringify(jsonData);
var options =
{
"method" : "post",
"contentType" : "application/json",
"payload" : payload
};
UrlFetchApp.fetch(postUrl, options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment