I am trying to fecth some data from a google spreadsheet in an AppScript standalone file.
I tried with this piece of code, but it is definitely incomplete. It raises this error:
TypeError: Cannot read property 'getDataRange' of undefined
My function:
var sheet=SpreadsheetApp.openById('1n2a5iecKo7seMXcef81V_pxWSRjE4agQ9DSoROtf0').getSheets()[0]
function getColumnId(sheet, namecol) {
var data = sheet.getDataRange().getValues();
var col = data[0].findIndex((name) => name === namecol) + 1;
return col;
}
After extracting the column data, I want to store that data in a normal list:
var IDCol=getcolumnId(sheet, 'ID')
var AFCol=getcolumnId(sheet, 'AF')
Is that possible in this way? I'd do debugging but I do not know how to proceed first with my function.
I believe your goal is as follows.
getColumnId(sheet, namecol).getColumnId. But you are trying to call the function with getcolumnId. I think that an error occurs.var sheet=DriveApp.getFileById('1n2a5ieOMcKo7seMXcef81V_pxWSRjE4agQ9DSoROtf0').getBlob() is Blob. In your script, an error occurs at var data = sheet.getDataRange().getValues().col. For thisfunction getColumnId(sheet, namecol) {
var data = sheet.getDataRange().getValues();
var col = data[0].findIndex((name) => name === namecol);
var transpose = data[0].map((_, c) => data.map(r => r[c])); // Here, "data" is transposed.
return transpose[col];
}
// Please run this function.
function sample() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Please set the sheet name.
var IDCol = getColumnId(sheet, 'ID')
var AFCol = getColumnId(sheet, 'AF')
console.log(IDCol)
console.log(AFCol)
}
When this modified script is run, the values of columns with the header values of ID and AF.
From your following replying and your updated question,
Hi Tanaike, I will definitely use your approach mapping the values as you said. But first of all I need to read the data from gsheet properly. Please see my Edit and the error is raised. My code is not place in the google spreadsheet itself but in a StandAlone file in AppsScript
I tried with this piece of code, but it is definitely incomplete. It raises this error:
TypeError: Cannot read property 'getDataRange' of undefined
If you are trying to directly execute the function getColumnId(sheet, namecol) by the script editor, such an error occurs, because sheet is undefined. I thought that this might be the reason for your current issue. From this situation, I thought that you might want to use sheet, IDCol and AFCol as the global variable. If my understanding is correct, how about the following sample script?
var sheet = SpreadsheetApp.openById('###').getSheets()[0];
function getColumnId(sheet, namecol) {
var data = sheet.getDataRange().getValues();
var col = data[0].findIndex((name) => name === namecol);
var transpose = data[0].map((_, c) => data.map(r => r[c]));
return transpose[col];
}
var IDCol = getColumnId(sheet, 'ID')
var AFCol = getColumnId(sheet, 'AF')
// Please run this function.
function myFunction() {
console.log(IDCol)
console.log(AFCol)
}
myFunction is run, you can see the values at the log.