如何使用数组值作为变量名?

时间:2013-02-17 07:52:02

标签: google-apps-script

我在这里有一段脚本创建了一个对象构造函数,然后buildList()从电子表格中获取数据并吐出一个包含部门名称的数组。我在buildObjects()尝试做的是使用该数组为每个部门创建一个对象,然后用与该部门相关的各种值填充它。显然我遇到了var ADCnames[i] = new ADC(ADCnames[i]);

行的问题
function ADC(name) {
  this.name = name;
}

function buildObjects () {
  var ADCnames = buildList('ADC', 1);
  var ADCarray = [];
  for (var i in ADCnames) {
    var ADCnames[i] = new ADC(ADCnames[i][0]);
    ADCarray.push(<<the variable I just created>>);
  }
  return ADCarray;
}

我是新手使用对象,所以可能有一种我想念的简单方法。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:2)

您所要做的就是重命名变量。

尝试

function buildObjects () {
  var ADCnames = buildList('ADC', 1);
  var ADCarray = [];
  for (var i in ADCnames) {
    var tempADC = new ADC(ADCnames[i][0]);
    //do whatever else you need to do to this object.
    ADCarray.push(tempADC);
  }
  return ADCarray;
}

答案 1 :(得分:1)

对象属性可以用点表示法或括号表示法(如数组)

指定

所以试试这个:

这[name] = name;

然后您不会获得部门名称的“变量”,但部门数组将是具有部门作为属性的对象。这是你想要的,如果部门名称永远是唯一的,你可能不需要数组用于厕所。

<强>更新

为了扩展我上面关于不需要额外数组的观点。因为您正在处理(我假设)唯一的部门名称,所以这些可能是关键。

所以:

function buildDepartments() {

  var departmentList = SpreadsheetApp.getActiveSpreadsheet().getRange('a1:c2').getValues(), // Assuming a 3 column list with, say: department name, department head, department deputy.
      departments = {},
      d;

  for (d = 0; d < departmentList.length; d += 1) {
    departments[departmentList[d][0]] = {head: departmentList[d][1], deputy: departmentList[d][2]};
  }

  // To recall the department details ... however many there are
  Logger.log(departments['Math'].head); // Logs the head of the Math department
}

但是,将自己绑定到可能随时间变化的密钥可能更有意义,就像部门名称更改一样,进行更改将是一件麻烦事。如果你“指定”对象,那么利用ScriptDB也会更容易。 因此:

function buildDepartments() {

  // Assuming a 3 column list with, say: name, head, deputy.
  var departmentList = SpreadsheetApp.getActiveSpreadsheet().getRange('a1:c2').getValues(), 
      departments = [], // Now this an array
      d;

  for (d = 0; d < departmentList.length; d += 1) {
    departments.push({
      title: departmentList[d][0],
      head: departmentList[d][1], 
      deputy: departmentList[d][2]
    });
  }

  // To recall the department details ... however many there are
  // Logs the head of the Math department by filtering the array to a single entry
  Logger.log(departments.filter(function (d) { return d.title = 'Math'; })[0]);
}

此时,为了让事情更直观一些召回,departments数组可能再次成为构造函数对象。因此:

function DepartmentList () {
  var list = [];
  this.addDepartment = function (dept) {
    list.push({ title: dept[0], head: dept[1], deputy: dept[2] });
    return this; // for chaining reasons, if you like.
  }
  this.getDepartment() = function(dept) {
    var foundDepartment = list.filter(function (d) { return d.title = dept; });
    return foundDepartment[0] || {}; // empty object if no department match
  }
  return list; // bare calls to the object returns the whole list
}

function buildDepartments () {
 var departments = SpreadsheetApp.getActiveSpreadsheet().getRange('a1:c2').getValues(),
     departmentList = new DepartmentList(), // Now this an object again
     d;

  departments.forEach( departmentList.addDepartment(entry) ); // Possible because GAS supports ECMAScript5 

  // To recall the department details ... however many there are
  // Logs the head of the Math department
  Logger.log(departmentList.getDepartment('Math').head);
}

如果输入数据更广泛或者需要对象函数进行任何处理,那么后者的metod才会有意义。对于我陈述的例子,这可能有点过分了。