简体   繁体   中英

VBA to Google Doc Apps Script

I generally program in VBA, but my company is moving towards google docs to replace Excel. My question stems around Object and Collections. In VBA, I am able to create a Class Module and create an object. It looks like I can't do that in Apps Script. Further, in VBA, I simply declare a collection inside of a sub like dim Col as new collection . However, I can't seem to get that right in my Apps Script. Below is my current code. I am trying to loop through line items, saving the line items as an object and storing that object in a collection with a key. Any help would be great! Right now when I compile the below code, it says the collection() is undefined.

    function Create_Collection(){

  var DFws = SpreadsheetApp.getActive().getSheetByName('Data Feed')
  var KPws = SpreadsheetApp.getActive().getSheetByName('KP')
  var DF_lastrow
  var KP_lastrow
  var CycleDate
  var DF_Date
  var i
  var Ingcol = new Collection();

  CycleDate = KPws.getRange(1,2).getValue();  //Sets which cycle ingredients need to be collected

  DF_lastrow = DFws.getLastRow();

  for (i = 2; i<= DF_lastrow; i++){

    DF_Date = DFws.getRange(i,1).getValue();

    if(DF_Date.toString() == CycleDate){

      var IngObj = { } // empty object
      IngObj.KitchenID = DFws.getRange(i,22).getValue();   // Added property KitchenID
      IngObj.Recipe = DFws.getRange(i,3).getValue();   // Added property Recipe
      IngObj.Name = DFws.getRange(i,4).getValue();   // Added property Name

      Ingcol.add (IngObj.KitchenID,IngObj)

      }



}

You don't have collection defined anywhere.
JavaScript is not based on classes but on prototypes so I don't think you need to do what you are trying to do.

Just initialize Incol like this:

var Incol = {}

and add objects like this:

Ingcol[IngObj.KitchenID] = IngObj;

like you did with IngObj if you want an associative array.

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