简体   繁体   English

在 Google App Script 中存储数组并将数组打印到 Google Sheets 列

[英]Storing an Array in Google App Script and Printing an Array to Google Sheets Column

I'm working on a Google Sheets and I'm trying to store an column of integers in an array, then clear a column on the sheet and then print the array of integers to a column on the sheet.我正在处理 Google 表格,我试图将一列整数存储在一个数组中,然后清除工作表上的一列,然后将整数数组打印到工作表上的一列。 Please see below for code...请参阅下面的代码...

function myFunction1() {

//Declaring the Active Sheet
var mySheet = SpreadsheetApp.getActiveSheet();

//Declaring the range that will make up the Array
var myRange = mySheet.getRange("E10:E328");

//Declaring Array 
var myArray = [[myRange.getValues()]];

//Clearing a range on the Google Sheet
mySheet.getRange('A10:A328').clearContent();

//Printing Array to Google Sheet
mySheet.getRange(15, 5).setValues([myArray]);

}

The above code runs without any errors but does not print my array to the sheet.上面的代码运行时没有任何错误,但不会将我的数组打印到工作表上。 I've been working on this for a while and have used the following articles to try to fix it...我已经研究了一段时间并使用以下文章尝试修复它......

https://productforums.google.com/forum/#!topic/docs/t99laH0rFc0 https://productforums.google.com/forum/#!topic/docs/t99laH0rFc0

Incorrect Range Height - Google Script 范围高度不正确 - Google Script

Writing 1D array into a sheet column in Apps Script 将一维数组写入 Apps 脚本中的工作表列

https://developers.google.com/apps-script/reference/spreadsheet/range#setvaluesvalues https://developers.google.com/apps-script/reference/spreadsheet/range#setvaluesvalues

Common errors that i have had when writing the code up to this point is, "Incorrect Range Height" and "Cannot convert Array to Object[][]".到目前为止,我在编写代码时遇到的常见错误是“范围高度不正确”和“无法将数组转换为对象 [][]”。

How would i fix this so that my Array prints to the column on the sheet?我将如何解决这个问题,以便我的 Array 打印到工作表上的列?

Thanks for your time!感谢您的时间! Any help on this would be great!!对此的任何帮助都会很棒!!

You're making it a bit more complex than you have to.你让它变得比你必须的更复杂。

function myFunction1() {

//Declaring the Active Sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();  
var mySheet = ss.getActiveSheet();

//Declaring the range that will make up the Array
var myArray = mySheet.getRange("E10:E328").getValues();//already in an array

//Clearing a range on the Google Sheet
mySheet.getRange('A10:A328').clearContent();

//Printing Array to Google Sheet
mySheet.getRange(15, 5).setValues(myArray); //already in an array don't need brackets

}

 // Write data in cell G1, H1, I1 function arrayPlay() { var newArray = []; // Variable that saves the data from G1->I1. var cellIn1 = ("G1"); var cellIn2 = ("H1"); var cellIn3 = ("I1"); var sheet = SpreadsheetApp.getActiveSheet(); // Defines where the data is recieved. (G1, H1, I1) var cellOut1 = sheet.getRange(cellIn1); var cellOut2 = sheet.getRange(cellIn2); var cellOut3 = sheet.getRange(cellIn3); // Recieve the data from those cells var data1 = cellOut1.getValue(); var data2 = cellOut2.getValue(); var data3 = cellOut3.getValue(); // Puts the data in the Array. (newArray) newArray.push(data1, data2, data3) // Presents the data in Cell 1-3 (A1, A2, A3)) Downwards sheet.appendRow([newArray[0]]); sheet.appendRow([newArray[1]]); sheet.appendRow([newArray[2]]); // Presents the data in Cell 1-3 (A1, B1, C1) Sideways sheet.appendRow([newArray[0], newArray[1], newArray[2]]); Logger.log(newArray); }

It is hard to tell exactly what you are trying to do.很难确切地说出您正在尝试做什么。 But if you are trying to replace column A with column E, just do this:但是,如果您想用 E 列替换 A 列,请执行以下操作:

function myFunction1() {
var mySheet = SpreadsheetApp.getActiveSheet();
var myRange = mySheet.getRange("E10:E328").getValues();
mySheet.getRange('A10:A328').setValues(myRange);
}

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

相关问题 Google Sheets App Script中数组的长度 - Length of an array in Google Sheets App Script Google Sheets App Script:您可以将数组写入单行而不是单列吗? - Google Sheets App Script: Can you write an array to a single row instead of a single column? Google表格脚本复制值从数组到列 - Google Sheets Script Copy Values from Array to Column getValues() Google Sheets 应用脚本不起作用 - 返回单个数组 - getValues() Google Sheets App Script not working - Returns a single array 谷歌表格脚本数组长度返回 Null - Google Sheets Script Array Length Returning Null 取消选中数组中的复选框(Google Apps 脚本 - 表格) - Uncheck checkboxes in an array (Google Apps Script - Sheets) Google Sheets Script - 循环遍历所有工作表,并将某一列的所有值添加到数组中,然后计算唯一值 - Google Sheets Script - Looping through all sheets, and adding all values of a certain column into array to then count unique values Google表格 - 查找数组中包含数据的最后一列 - Google Sheets - Find last column with data in array 在 Google 表格单元格 (GAS) 中打印未指定数量的数组内容 - Printing Unspecified Number of Array Contents in Google Sheets Cells (GAS) 如何使用谷歌应用脚本将数组写入工作表列并将自定义公式添加到旁边的列? - How to write array to sheets column and add custom formula to column next to it using google apps script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM