Skip to content

Instantly share code, notes, and snippets.

@jollychang
Created March 16, 2021 11:25
Show Gist options
  • Save jollychang/b86835e70ad45c7f2f125cdd342e305f to your computer and use it in GitHub Desktop.
Save jollychang/b86835e70ad45c7f2f125cdd342e305f to your computer and use it in GitHub Desktop.
Google App Script funtions
//https://yagisanatode.com/2018/10/05/google-apps-script-get-file-by-name-with-optional-parent-folder-crosscheck/
function getFileByName(fileName, fileInFolder){
var filecount = 0;
var dupFileArray = [];
var folderID = "";
var files = DriveApp.getFilesByName(fileName);
while(files.hasNext()){
var file = files.next();
dupFileArray.push(file.getId());
filecount++;
};
if(filecount > 1){
if(typeof fileInFolder === 'undefined'){
folderID = {"id":false,"error":"More than one file with name: "+fileName+". \nTry adding the file's folder name as a reference in Argument 2 of this function."}
}else{
//iterate through list of files with the same name
for(fl = 0; fl < dupFileArray.length; fl++){
var activeFile = DriveApp.getFileById(dupFileArray[fl]);
var folders = activeFile.getParents();
var folder = ""
var foldercount = 0;
//Get the folder name for each file
while(folders.hasNext()){
folder = folders.next().getName();
foldercount++;
};
if(folder === fileInFolder && foldercount > 1){
folderID = {"id":false,"error":"There is more than one parent folder: "+fileInFolder+" for file "+fileName}
};
if(folder === fileInFolder){
folderID = {"id":dupFileArray[fl],"error":false};
}else{
folderID = {"id":false,"error":"There are multiple files named: "+fileName+". \nBut none of them are in folder, "+fileInFolder}
};
};
};
}else if(filecount === 0){
folderID = {"id":false,"error":"No file in your drive exists with name: "+fileName};
}else{ //IF there is only 1 file with fileName
folderID = {"id":dupFileArray[0],"error":false};
};
return folderID;
};
function wechat(wechat_bot_url, message_text){
var subscribe = {
"msgtype": "text",
"text": {
"content": message_text
}
};
var options = {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(subscribe)};
var res = UrlFetchApp.fetch(wechat_bot_url, options);
console.log(res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment