简体   繁体   中英

Create Folder from new Google sheet Row

Hope that somebody can help me. I create a Google script for New Folder include Hyperlink. (very new in Script)

For some reason it gives no Errors but it also creates No Folders.. The Hyperlink must be in Column R and the Name of the Folder is from the last row Column a,b together.

(is there also a possibility to Copy a existing Template Folder to a New Folder)

function createAndHyperlink() {
    var ss, sh, parent, parent2, r, v, thisCell, folder
    ss = SpreadsheetApp.getActive()
    sh = ss.getSheetByName('MOC Permanent')
    parent = DriveApp.getFileById(ss.getId()).getParents().next();
    parent2 = DriveApp.getFolderById("1vxS3FebsDLqrGGQc9kG7cE3tO9zN8Qre")
   r = sh.getLastRow()
   v = sh.getMaxColumns()
      for (var i = 0, l = v.length; i < l; i++) {
        thisCell = sh.getRange(i + 3, 2)
        if (v[i][0] && !thisCell.getFormula()) {
            folder = parent2.createFolder(v[i][0]);
            thisCell.setFormula('=HYPERLINK("' + folder.getUrl() + '"; "' + v[i][0] + '")');
        }
    }
}

Google Sheet: https://docs.google.com/spreadsheets/d/12n2Cmu6OqZskD_j7YhVrU14qaQPPGnQyIoSEIw8stqE/edit?usp=sharing

Hope somebody can help me

If I understand your post correctly, you want to create Sub folders named after every row data on column A + Column B inside of your Parent Folder. Then, get each link of the Sub Folders and add each of these links on Column R as HYPERLINKS .

RECOMMENDATION

I have tweaked your current script and you may use this sample script below as reference:

UPDATED

//MAIN SCRIPT TO RUN
function createAndHyperlink() {
    var ss, sh, lastRow, name;
    ss = SpreadsheetApp.getActive();
    sh = ss.getSheetByName('MOC Permanent');
    lastRow = sh.getDataRange().getLastRow();

    for(var row=2; row<=lastRow; row++){
      name = sh.getRange(row,1).getValue()+sh.getRange(row,2).getValue();
      if(sh.getRange(row,18).getValue().toString()==""){
        sh.getRange(row,18).setFormula('=HYPERLINK('+"\""+createFolder(name).getUrl()+"\",\""+name+"\")");
        Logger.log("Created a folder for "+"Name:\n"+name);
      }else{
        Logger.log("R"+row+" contains a hyperlink already!");
      }
    }

    //THIS CODE BELOW ENSURES THE LAST ROW OF COLUMN R WAS ADDED WITH HYPERLINK BEFORE COPYING FUNCTION BEGINS
    if(sh.getRange(lastRow,18).getValue().toString()!=""){
      addTempFileCopy();
    }
}

//FUNCTION TO CREATE SUB FOLDERS INSIDE THE PARENT FOLDER
function createFolder(folderName){
    var parent, folders, firstLevelFolder, newfolder;

    parent = DriveApp.getFolderById("PARENT_FOLDER_ID").getName();
    folders = DriveApp.getFoldersByName(parent);
    firstLevelFolder = (folders.hasNext()) ? folders.next() : DriveApp.createFolder(parent);

    //CREATE UNIQUE SUBFOLDERS INSIDE OF THE PARENT FOLDER
    newfolder = firstLevelFolder.createFolder(folderName);
    return newfolder; //RETURNS THE LINK OF THE CREATED SUBFOLDER
}

//FUNCTION TO ADD THE TEMPLATE FILE INTO EACH CREATED SUB FOLDERS
function addTempFileCopy(){
    var parent, subFolder, ss;

    ss = SpreadsheetApp.getActive();
    parent = DriveApp.getFolderById("PARENT_FOLDER_ID").getFolders();

    while(parent.hasNext()) {
      subFolder = parent.next();
      var destFolder = DriveApp.getFolderById(subFolder.getId()); 
      DriveApp.getFileById(ss.getId()).makeCopy(subFolder.getName()+" - Template file copy", destFolder);
      Logger.log("Template file name: "+ss.getName()+"\nAdded to folder: "+subFolder.getName());
    }

}

SAMPLE SHEET

在此处输入图像描述

RESULT

After running the script, HYPERLINKS were added on the on COLUMN R :

在此处输入图像描述

Here are the created Sub folders on Google Drive inside of the Parent folder:

在此处输入图像描述

Execution log result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM