0
votes

I hope I can explain this clearly.

I have a Googlesheet with a "Ticket" Sheet and a "Customner Data" Sheet. Ticket sheet has Column A as "Ticket Number", Column B as "Customer Name" Customer Data Sheet has column A Customer number, Column C "First Name", Column D "Last Name".

I have a script that as each customer is added to a new row it creates a folder in a folder in drive in the format "Lastname Firstname - Customernumber"

What I am trying to do is as a new row is added to the "Ticket" sheet the script can find the customer folder then add child folder named "TicketNumber".

Here is the code I have been trying so you can see where I am stuck. Thank you

    //Global

var googleFolderID = ('DRIVE FOLDER ID');
var ss = SpreadsheetApp.getActive();

function createTicketFolder() {


// identify the sheet where the data resides
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Tickets");

//Select the column we will check for the first blank cell
var columnToCheck = sheet.getRange("A:B").getValues();

// Get the last row based on the data range of a single column.
var lastRow = getLastRowSpecial(columnToCheck);

  //identify the cell that will be used to name the folder 
 var ticketNumber = sheet.getRange(lastRow,1,1,1);
  var customerNumber = sheet.getRange(lastRow,2,1,1);


//identify the parent folder the new folder will be in
var parentFolder=DriveApp.getFolderById(googleFolderID);

// Find Customer folder within parentFolder, formatted as "Lastname Firstname - CustomerNumber"
//Need Help here

//create the new folder
var newFolder=customerFolder.createFolder(ticketNumber);


};



function getLastRowSpecial(range){
  var rowNum = 0;
  var blank = false;
  for(var row = 0; row < range.length; row++){

    if(range[row][0] === "" && !blank){
      rowNum = row;
      blank = true;
    }else if(range[row][0] !== ""){
      blank = false;
    };
  };
  return rowNum;
};

The Google sheets The "Ticket" Sheet

The Ticket Sheet

The "Customer Data" Sheet

The Customer Data Sheet

Google Drive Folder This is how the google drive folders are formatted.

enter image description here

So from the sheets above I need to find the customer folder and add a subfolder with ticket number as folder name.

1
You say that "Ticket" Sheet contains the customer name, but in your code it is the customer number - var customerNumber = sheet.getRange(lastRow,2,1,1);. It will be easier to help you if you could provide a copy of your spreadsheet.ziganotschka
Ohh they both contain customernumber, I will make a test sheet to show.Tim

1 Answers

1
votes

You need to implement the following steps

  1. Retrieve the customer number from the last entry on Tickets sheet
  2. Find the row containing this number in the Customer Data sheet with indexOf()
  3. Retrieve First Name and Last Name in order to reconstruct the name of the customer folder
  4. Find the folder by name

Sample:

...
//IMPORTANT: retrieve the value!
  var ticketNumber = sheet.getRange(lastRow,1).getValue();
  var customerNumber = sheet.getRange(lastRow,2).getValue();  
  //identify the parent folder the new folder will be in
  var parentFolder=DriveApp.getFolderById(googleFolderID);
  var customerSheet = ss.getSheetByName("Customer Data");
  var data = customerSheet.getDataRange().getValues();
  // the following line retrieves all customerIds from the 2-D value range and saves them in the 1-D array "customerIds" - necessary for using indexOf()
  var customerIds = data.map(function(e){return e[0];}); 
  var index = customerIds.indexOf(customerNumber);
  // if the customer number has been found
  if(index >= 0){
    // Lastname Firstname - Customernumber"
    var folderName = data[index][3] + " " + data[index][2] + " - " + data[index][0]; 
    //  retrieve the first folder with this name - important: avoid duplicates!
    var customerFolder = parentFolder.getFoldersByName(folderName);//.getFoldersByName(folderName);
    if(customerFolder.hasNext()){
      customerFolder = customerFolder.next();
      //check if ticket folder exists
      var ticketFolder=customerFolder.getFoldersByName(ticketNumber);
      if(ticketFolder.hasNext()){
        var newFolder = ticketFolder.next();
       } else {
        // if it does not exist, create the new folder
        var newFolder=customerFolder.createFolder(ticketNumber);
       }
    }    
  }