admin管理员组文章数量:1391937
My code is meant to take data from a user input form and insert a new row to a spreadsheet:
function addNewRow(rowData) {
const currentDate = new Date();
const ss = SpreadsheetApp.getActiveSheet;
const ws = ss.getSheetByName('DataTable');
ws.appendRow(rowData.userName,rowData.userProject, rowData.workDate, rowData.timeSpent, currentDate);
}
Yet when I run the code it returns
TypeError: ss.getSheetByName is not a function
My code is meant to take data from a user input form and insert a new row to a spreadsheet:
function addNewRow(rowData) {
const currentDate = new Date();
const ss = SpreadsheetApp.getActiveSheet;
const ws = ss.getSheetByName('DataTable');
ws.appendRow(rowData.userName,rowData.userProject, rowData.workDate, rowData.timeSpent, currentDate);
}
Yet when I run the code it returns
Share Improve this question edited Sep 16, 2022 at 13:34 Wicket 38.8k9 gold badges80 silver badges195 bronze badges asked Apr 5, 2021 at 11:31 poppaGottzpoppaGottz 11 silver badge1 bronze badge 2TypeError: ss.getSheetByName is not a function
- If you log the ss variable, what is the output? – Gilles Heinesch Commented Apr 5, 2021 at 11:34
- It pletes without issue. – poppaGottz Commented Apr 6, 2021 at 13:02
2 Answers
Reset to default 3Use const ss = SpreadsheetApp.getActive()
to get the spreadsheet. Note the parentheses. The function will not get called if you omit them.
The error "getSheetByName
is not a function" occurs when running a function in Google Apps Script when this method is called upon a variable that wasn't assigned a Class Spreadsheet
object.
In this case, the OP forgot to include the parenthesis at the end of SpreadsheetApp.getActiveSheet
causing Spreadsheet.getActiveSheet
method to be assigned to the variable instead of executing this method to return a Class Sheet
object. This also happens when chaining another method that doesn't return a Class Spreadsheet
Object, i.e. Spreadsheet.getActiveSheet().getName()
returns a string.
In the OP case the solution is straightforward as already was show in the previous answer. Other cases having the same error might require a different solution, to find it,
- Check the correct spelling and syntax. For this it's very helpful to understand the pretty basics of JavaScript, i.e, JavaScript is case sensitive. This means that
getActiveSheet
is not the same asgetactivesheet
. - Check the official guides in https://developers.google./apps-script/overview, more specifically https://developers.google./apps-script/guides/sheets.
- Check the official reference in https://developers.google./apps-script/reference. In this specific case you might search for
getSheetByName
directly in this link. In this case this method is used in several examples that might be helpful to understand it. - Going deep in the Google Apps Script editor, use the autoplete feature to have instant feedback about it the method is available for the object assigned to the variable: Type a dot after the variable name, this will show a list of the available methods, continue typing the method name to filter the list suggestions, if there isn't any suggestion, then there is very likely that there is a problem on the variable declaration. Ref. https://developers.google./apps-script/guides/services/#using_autoplete
- Use the Google Apps Script debugger. Ref. https://developers.google./apps-script/guides/support/troubleshooting#debugging
- Go deep in learning JavaScript. There a lot of resources to start learning JavaScript. By default Google Apps Script use V8 to execute scripts. Please bear in mind that Web APIs like the DOM API aren't available in V8. It's worthy to note that there are some JavaScript functions that aren't supported in Google Apps Script like
async
as well some Global Objects likePromise
Related
- How do I check if an object has a specific property in JavaScript?
- Accessing spreadsheet in Google Script (this question is about a similar TypeError message)
- Automatically replace dots with mas in a Google Sheets Column with Google Script (this question is about a similar TypeError message)
本文标签: javascriptTypeError ssgetSheetByName is not a functionStack Overflow
版权声明:本文标题:javascript - TypeError: ss.getSheetByName is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744700750a2620547.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论