简体   繁体   English

分配给Google脚本中的数组

[英]assigning to an array in google script

This is probably an ignorant mistake on my part, but I am trying to assign different values from different column to an array to put in a master sheet. 就我而言,这可能是一个无知的错误,但是我试图将不同列中的不同值分配给要放入母版表的数组。 I would like the last column to be equal the the sheet name where the data came from to track it back. 我希望最后一列等于工作表名称,数据来自该工作表名称以进行追溯。

I feel I am close, but when I get to the last column which I want to be the sheet name I get the following error: Cannot convert Array to Object[][]. 我觉得我已经接近了,但是当我到达想要作为工作表名称的最后一列时,出现以下错误:无法将Array转换为Object [] []。 (line 19, file "combineTOmaster") (第19行,文件“ combineTOmaster”)

This is my code so far. 到目前为止这是我的代码。 I know the issue is the other column are array[[][]] and the last one is array [][] but I don't know how to fix it. 我知道问题是另一列是array [[] []],最后一列是array [] [],但我不知道如何解决。 Any ideas? 有任何想法吗?

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;
    }

Thank you 谢谢

Lets break your function down - 让我们分解一下功能-

// 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;
}

@ra89fi Thank you so much. @ ra89fi非常感谢。 You lead me to the answer. 你带我找到答案。 So mess up 1 was using = and not ==. 因此,混乱1是使用=而不是==。 Mess up two was using colIndex instead of label. 搞砸了两个是使用colIndex而不是标签。 Mess up three was not using push. 混乱三不使用推。 Is there a good resource on push? 是否有很好的推动资源? Thank you again!! 再次感谢你!!

Below is the final code that worked for me. 以下是对我有用的最终代码。

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; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM