写入数组的一列或将 2 列数组转置为一列然后写入

Writing one column of an Array or transposing a 2 column array to a one column then writing

您好,我已经尝试了很多天来解决这个问题,但我做不到。我可以向你们寻求帮助吗?

我将 sheet 中的 2 列拉入一个数组,然后循环数组覆盖数组的第二列以成为 SQL 插入字符串。然后我将数组写入另一个 sheet。问题是我真的只想将数组的第 2 列写入新的 sheet。第一列不是必需的,它实际上占据了我需要的单元格,因为我拉取的价格历史将 google sheet 置于 5,000,000 个单元格限制之上。如果我只能写一个专栏,它不会超过限制。

//this code is running in a loop returning each different stock in a list of tickers

//get 2 columns from the price history and put them in an array
  const PRICERANGE=SheetPriceHistory.getDataRange();
  var PRICEARRAY=PRICERANGE.getValues();

//append the output of the the entire array to the other sheet.  
SheetScript.getRange(SheetScript.getLastRow()+1,1,PRICEARRAY.length,PRICEARRAY[0].length).setValues(PRICEARRAY); 
// this is where I would like to write just column 2 of the array to the SheetScript sheet.

前提是你的数据位于sheetSheetPriceHistory的A列和B列,你可以选择性的检索第一列

为此,请将请求 getDataRange() 替换为 getRange(row, column, numRows, numColumns)

因此,如果您只想检索一列,那么 numColumns 将是 1,并且可以使用 getLastRow().

检索行数

所以:

 const PRICERANGE=SheetPriceHistory.getRange(1, 1, SheetPriceHistory.getLastRow(), 1);
  var PRICEARRAY=PRICERANGE.getValues();

//append the output of the the entire array to the other sheet.  
SheetScript.getRange(SheetScript.getLastRow()+1,1,PRICEARRAY.length,PRICEARRAY[0].length).setValues(PRICEARRAY); 

更新

如果您想检索一个两列数组并稍后删除第一列,您可以使用 map()

样本:

  const PRICERANGE=SheetPriceHistory.getRange(1, 1, SheetPriceHistory.getLastRow(), 2);
  var PRICEARRAY=PRICERANGE.getValues();
  ... //do something
  //now overwrite the array with only the second column
  PRICEARRAY = PRICEARRAY.map(function (e) { 
    return [e[1]]; 
  })
  //append the output of the the entire array to the other sheet.  
  SheetScript.getRange(SheetScript.getLastRow()+1,1,PRICEARRAY.length,PRICEARRAY[0].length).setValues(PRICEARRAY);