[英]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.