[英]How do I extract data from certain columns in AppScript?
I am trying to fecth some data from a google spreadsheet in an AppScript standalone file.我正在尝试从 AppScript 独立文件中的谷歌电子表格中提取一些数据。
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(sheet, namecol)
从列中检索值。getColumnId
.getColumnId
。 But you are trying to call the function with getcolumnId
.getcolumnId
调用该函数。 I think that an error occurs.var sheet=DriveApp.getFileById('1n2a5ieOMcKo7seMXcef81V_pxWSRjE4agQ9DSoROtf0').getBlob()
is Blob. var sheet=DriveApp.getFileById('1n2a5ieOMcKo7seMXcef81V_pxWSRjE4agQ9DSoROtf0').getBlob()
是 Blob。 In your script, an error occurs at var data = sheet.getDataRange().getValues()
.var data = sheet.getDataRange().getValues()
处发生错误。col
.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
.运行此修改后的脚本时,标题值为
ID
和AF
的列的值。
From your following replying and your updated question,从您的以下回复和更新的问题中,
Hi Tanaike, I will definitely use your approach mapping the values as you said.
嗨 Tanaike,我一定会使用你的方法来映射你所说的值。 But first of all I need to read the data from gsheet properly.
但首先我需要正确读取 gsheet 中的数据。 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
我的代码不在 Google 电子表格本身中,而是在 AppsScript 中的 StandAlone 文件中
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.如果您尝试通过脚本编辑器直接执行函数
getColumnId(sheet, namecol)
,则会出现这样的错误,因为sheet
未定义。 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.从这种情况来看,我认为您可能希望使用
sheet
、 IDCol
和AFCol
作为全局变量。 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.myFunction
运行时,你可以在日志中看到这些值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.