This is my first time using the google sheets API, and I'm trying to write a script that takes in some data and processes it, which works, and then I want to write the processed data to a column in an already existing sheet, which it what I need help with.
Is there some better way to put an array in a specific range?
The error message says cannot get range of null, this is my code so far, the line that isn't part of the block is the issue:
function getList() {
var spreadsheetId = '1BjnZ-SPBVcCdITxcvEX1nCPecvGdYSKQ0m_GiaKduZQ';
var rangeName = 'Paste Here!I1:GZ6';
var pasteRange = 'The Goods!B3:B1000';
var values = Sheets.Spreadsheets.Values.get(spreadsheetId, rangeName).values;
var width = values[0].length;
var teamNumbers = [];
if (!values) {
Logger.log('No data found.');
} else {
for(var col = 1; col < width; col+=2) {
for(var row = 0; row < values.length; row++) {
teamNumbers += values[row][col].toString();
teamNumbers += '\n'
}
}
SpreadsheetApp.getActiveSpreadsheet().getRange(pasteRange).setValues(teamNumbers);
}
}
Also, secondary question, how can you make the range for something a variable, as in in order to get I1:GZ6 from a parameter?
Any help is much appreciated, thanks in advance!
The only reason you would get the error
from the line
SpreadsheetApp.getActiveSpreadsheet()
is if you are using a standalone script instead of a bound script. Standalone scripts must useSpreadsheetApp.openById("some id")
in order to access a spreadsheet.For more information on the differences, consult the Apps Script documentation
Standalone scripts
SpreadsheetApp#getActiveSpreadsheet
For your 1st question
How about this modification?
Modification points :
teamNumbers
is declared as an array. But it is used as string.teamNumbers
has to be used as the 2 dimensional array.teamNumbers = [[value]]
.setValue(teamNumbers)
ofSpreadsheetApp
, please useteamNumbers = value
.SpreadsheetApp.getActiveSpreadsheet().getRange(pasteRange).setValues(teamNumbers)
,pasteRange
is required to be matched to the size of rows and columns ofteamNumbers
.var rangeName = 'Paste Here!I1:GZ6'
, you can useSheets.Spreadsheets.Values.update()
of Sheets API.Modified script :
Note :
Reference :
For your 2nd question
If you want to convert the gridrange to a1notation, you can see a sample at the document and this gist.
If I misunderstand your question, I'm sorry.