分配给Google脚本中的数组

时间:2019-03-09 07:31:46

标签: arrays google-apps-script

这对我来说可能是一个无知的错误,但是我试图将不同列中的不同值分配给要放入母版表的数组。我希望最后一列等于工作表名称,数据来自该工作表名称以进行追溯。

我觉得我已经接近了,但是当我到达要用作工作表名称的最后一列时,出现以下错误:无法将Array转换为Object [] []。 (第19行,文件“ combineTOmaster”)

到目前为止,这是我的代码。我知道问题是另一列是array [[] []],最后一列是array [] [],但我不知道如何解决。有任何想法吗?

function getColValues(label,sheetName) {

  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  var colIndex = getColumnIndex(label, sheetName);
  var numRows = ss.getLastRow() -1;
  if (colIndex > 0 ) { 
    var colValues = ss.getRange(2, colIndex, numRows, 1).getValues();
  } 
  else if (colIndex = "orginalsheet") { 
    var colValues = [];
    var array = [];
    for (var i = 0; i < numRows-1;i++) {
      array[i] = sheetName;
    }  
    colValues = array;
  }
      else {
    var colValues = [];
  }
  Logger.log(colValues);
  return colValues;
    }

谢谢

2 个答案:

答案 0 :(得分:0)

让我们分解功能-

// you're passing column header and sheet name, OK
function getColValues(label, sheetName) {
  // getting sheet, OK
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);

  // getting column index, OK
  var colIndex = getColumnIndex(label, sheetName);

  // getting number of rows to collect, OK
  var numRows = ss.getLastRow() - 1;

  // at this point, i assume, variable colIndex is a number, because it should be
  // if label column exists, colIndex will be greater than 0, OK
  if (colIndex > 0) {
    // get values of that column, OK
    var colValues = ss.getRange(2, colIndex, numRows, 1).getValues();
    // colValues will be like this, at this point-
    // [ [value], [value], [value] ... ]

  } else if ((colIndex = 'orginalsheet')) {
    // NOT OK, = sign assigns 'orginalsheet' to colIndex
    // do you wanna assign or compare?
    // to compare, use == sign
    // besides why are you assigning a string value to colIndex ?
    // colIndex should be a number, in my understanding

    // now, what do you wanna do here ?
    var colValues = [];
    var array = [];
    for (var i = 0; i < numRows - 1; i++) {
      array[i] = sheetName;
    }
    colValues = array;
    // at his point, colValues is -
    // [sheetName, sheetName, sheetName...]

    // is this correct colValues ?
    // i guess not
    // i think you're trying to use sheetName as a column value for every row
    // if that is the case, do this
    var colValues = [];
    for (var i = 0; i < numRows; i++) colValues.push([sheetName]);
    // now colValues is -
    // [ [sheetName], [sheetName], [sheetName].... ]
    // which you can insert in a sheet as values of a column
  } else {
    var colValues = [];
  }
  Logger.log(colValues);
  return colValues;
}

答案 1 :(得分:0)

@ ra89fi非常感谢。你带我找到答案。因此,混乱1是使用=而不是==。搞砸了两个是使用colIndex而不是标签。混乱三不使用推。是否有很好的推动资源?再次感谢你!!

下面是对我有用的最终代码。

function getColValues(label,sheetName) {

  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  var colIndex = getColumnIndex(label, sheetName);
  var numRows = ss.getLastRow() -1;
  if (colIndex > 0 ) { 
    var colValues = ss.getRange(2, colIndex, numRows, 1).getValues();
  } 
  else if (label == "orginalsheet") { 
    var colValues = [];
    for (var i = 0; i < numRows;i++) {
      colValues.push([sheetName]);
    }  
  }
      else {
    var colValues = [];
  }
  return colValues; 
}