Monday, June 16, 2014

Bulk Reading and Writing

var names = new Array();
#####################################################5000 records in under a second..#####################################################function bulkscan() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getActiveSheet();
var range = sh.getRange(2, 16, 5800, 1);
//row, column, numrows, numcol)
//Create an array and store all of the values in it
var names = range.getValues();

for (i=0; i < names.length; ++i)
{
//this represents the first ("0") line of a multidimensional array
if (names[i][0].toString().trim().toUpperCase() == "REPLACE")
{
Logger.log("The Row is " + (i+2));
Logger.log("The domain is " + sh.getRange((i+2), 9).getValue());
}
}
}

#####################################################A HORIZONAL ARRAY VS A VERTICAL ARRAY#####################################################IF you are scanning like 1 row ACROSS you need to do this...  
for (i=0; i < header[0].length; ++i)   
{    Logger.log(header[0][i].toString().trim().toUpperCase());    
//this represents the first ("0") line of a multidimensional array    
if (header[0][i].toString().trim().toUpperCase() == "DOMAIN")

HOWEVER, If you are scanning like a long column down you need to do this..  
for (i=0; i < names.length; ++i)   
{    //this represents the first ("0") line of a multidimensional array    
if (names[i][0].toString().trim().toUpperCase() == "REPLACE")    dafd

notice the difference in the usage of the [0][i] vs the [i][0]









Friday, June 6, 2014

Arrays and single line IF statement

//declare your array
var names = new Array();

//add to your array
names.push("Add me");

//WHERE in the array is the item located
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple"); //the "O" is CAPITAL!!
//the result of the above is a = 2

//to search backwards from end of array do this..
var a = fruits.lastIndexOf("Apple");
//the result of the above is a = 2


#####################################################
A LOOP TO BUILD AN ARRAY...
#####################################################
//loop through each item and build an array storing the domain index keys
while (ReplaceRange.getRow() <= lRow)
{
ReplaceRange = ReplaceRange.offset(1,0);
ReplaceRange.activate();
if (ReplaceRange.getValue() != "")
{
Logger.log("new Domain is " + ReplaceRange.getValue());
names.push(ReplaceRange.getValue());
}
}


#####################################################
A GENERAL FOR LOOP TO LOOP THROUGH EXISTING..
#####################################################
for (i=0; i < names.length; ++i) 
{
//this represents the first ("0") line of a multidimensional array
var SingleItem = names[i][0].toString().trim();

//this assigns a single item from the array to a variable
var SingleItem = names[i].toString().trim();
}


#####################################################
A FOR LOOP TO CHECK IF AN ARRAY CONTAINS AN ITEM
#####################################################
//loop through each item in the array of domain names      
for (var i=0; i<names.length; i++)
{
Logger.log("The Single item is  " + SingleItem);
Logger.log("The Array value is " + names[i]);
if (SingleItem == names[i])
{
//TRUE.. VARIABLE IS IN ARRAY
}
} //END of for loop

#####################################################
SINGLE LINE IF STATEMENT
#####################################################
var Name1 = (FormInfo[0][4] != "")?1:0;
Name1 = (FormInfo[0][5] != "")?Name1+1:Name1;


//This gets every value in a row, adds items to each element and then puts it back
//get every value in the sheet
//in potions 3 and 4 of the get range 1 = same row/colmum.  The following example starts in the current row column B and goes over to the current row column F.
//(1,1,1,1) is A1 as a cell
//(1,1,1,3) is A1:C1)
var current = TwoSheet.getRange(TwoRange.getRow(), 2, 1, 5).getValues();
current[0][0] = current[0][0] + Business;
current[0][1] = current[0][1] + Lumber;
current[0][2] = current[0][2] + Refferal;
current[0][3] = current[0][3] + Name1;
current[0][4] = current[0][4] + Visitor1;
           
//rewrite the sheet back out
TwoRange = TwoSheet.getRange(TwoRange.getRow(), 2, 1, 5).setValues(current);


Code to replace all FORM items

///////////////////////////////////////////////
    // INITIALIZE MULTI-DIMENSIONAL ARRAY
    // [NAMED RANGE, NAME ON FORM, TYPE OF ELEMENT]
    ///////////////////////////////////////////////
    var array = [["Members","Who are you?","asListItem"],
                 ["Members","One2One's with members of our group","asListItem"]];
    var formID = '1gqkjPbC902ahDZq5Hdud2BlYepuBos_fe4CvKdsfasdq5M' //this is the ID of the form you are using
        
    var ss = SpreadsheetApp.getActive();
    //var sheet = ss.getSheetByName("Members");
    //var range = sheet.getRange(2, 1, 98, 1);
    //range.sort(1);
  
    // open the form and then get all items from inside the form.   
    var form = FormApp.openById(formID);
    var AllFormItems = form.getItems();
    form.setTitle(ss.getRangeByName("FormName").getValue()); //RENAME THE FORM HERE
    var FormItemId = 0; 
  
    
    // Open the current spread sheet
    var ss = SpreadsheetApp.getActive();
   
    // LOOP THROUGH ALL ITEMS ONE AT A TIME
    for (var n = 0; n < array.length; n++) 
    {
    
      // Getting the items in your named range from the spreadsheet
      var SheetItemRange = ss.getRangeByName(array[n][0]);
      var SheetItems = SheetItemRange.getValues();
   
      // loop through all of the items in the form
      for (i=0; i < AllFormItems.length ; ++i) 
      {
        var FormTitle = AllFormItems[i].getTitle();
    
        // Compare the title to that of each item in your form
        if (FormTitle == array[n][1])
        {
          FormItemId = AllFormItems[i].getId();
        }
      }
      
      // If the item was not found then exit
      if (FormItemId == 0) {
        return; 
      } 
      
      switch (array[n][2]) {
        case "asCheckboxItem":
          // Casting item to checkboxItem because the form is using a checkbox 
          FormItemList = form.getItemById(FormItemId).asCheckboxItem();
          break;
        case "asListItem":
          // Casting item to listItem because the form is using a drop down. 
          FormItemList = form.getItemById(FormItemId).asListItem();
          break;
      }
      
      // Create the array of choices and loop through each of them
      var SheetItemChoices = []
      for (i=0; i < SheetItems.length; ++i) {
        
        SingleSheetItem = SheetItems[i][0].toString().trim();
        if (!(SingleSheetItem=='')) {
          SheetItemChoices.push(FormItemList.createChoice(SingleSheetItem));
        }
      }
      
      // Setting the choice array to drop down item.
      FormItemList.setChoices(SheetItemChoices);
    }