javascript - 谁能告诉我我的代码有什么问题?

标签 javascript google-apps-script syntax-error reserved-words

有人能告诉我我的代码有什么问题吗?我一直看到的错误是 Missing ;声明之前。 (第 3 行,文件“代码”)。我正在关注 YouTube 上的教程,但我不知道我的代码和导师之间有什么区别。

function createPDf () {    
  let studentname = "Example Smith";   
  let class = "S2H";    
  let year = "Year 4";    
  let Literacy = "Working at Stage outcomes";    
  let Numeracy: = "Working at Stage outcomes";    
  let socialinteractionsclassroom = "Usually";    
  let socialinteractionsplayground = "Usually";    
  let absences = "10";    
  let contactteacher = "yes";    
  let teacehername = "Mr Jacob Example";    
  let effort = "Usually";    

  // doc id 1c-DF4cCNu8Ma5khhL6FiMudmlc7E9XGrigpWPd-06lE    
  // temp folder 14I2Jql8BRaK3tPh0XNa8C9IoNkYuPRir    

  const docFile = DriveApp.getFileById("1c-DF4cCNu8Ma5khhL6FiMudmlc7E9XGrigpWPd-06lE");    
  const tempFolder = DriveApp.getFolderById("14I2Jql8BRaK3tPh0XNa8C9IoNkYuPRir");    
  const tempFile = docFile.makeCopy(tempFolder);    
  const tempDocFile = DocumentApp.openById(tempFile.getId());    
  const body = tempDocFile.getBody();    
  body.replaceText("{Student name:}", studentname);    
  body.replaceText("{Class:}", class);    
  body.replaceText("{Year}", year);    
  body.replaceText("{Teacher Name}", teacehername);    
  body.replaceText("{Literacy}", Literacy);    
  body.replaceText("{Numeracy}", Numeracy);    
  body.replaceText("{Social interactions in the classroom}", socialinteractionsclassroom);    
  body.replaceText("{Social interactions in the playground}", socialinteractionsplayground);    
  body.replaceText("{Effort}", effort);    
  body.replaceText("{Absences exlcuding COVID}", absences);    
  tempDocFile.saveAndClose();    

}

最佳答案

问题

尝试命名变量 class导致“Missing ; before statement”错误。

解释

ECMAScript 262 spec ,有一个关键字和保留字的列表。你 不能在为它们定义的任何其他上下文中使用它们(有些甚至为将来保留)。

保留字列表ATTOW

等待 |休息 |案例 |捕获|类(class) |常量 |继续 |调试器 |默认 |删除 |做 |否则 |导出 |延伸 |假 |最后 |为|功能 |如果 |进口 |在|实例 |新 |空 |返回 | super |开关 |这个 |扔|真实 |试试|类型 |变量 |无效 |而|与 |屈服

留作 future

枚举 |工具 |接口(interface) |包装 |私有(private) | protected |上市

关于错误消息

您会看到不太有用的错误消息,因为您的脚本在旧的 Rhino runtime 下运行。使用气体。如果您使用的是新版本 V8(请尽快执行 migrate),您会收到以下信息:

SyntaxError: Unexpected token 'class'



实际上,还有第二个问题会导致错误 - 使用冒号和 assignment operator不是有效的 JS 语法,但所有功劳都应该归功于另一个答案:

SyntaxError: Unexpected token ':'

关于javascript - 谁能告诉我我的代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62400578/

相关文章:

javascript - Rails 仅在页面重新加载后才加载我的 javascript

google-apps-script - 在第一个计数实例上方添加一行 - Google 电子表格

javascript - JSON.parse() 不适用于变量类型字符串,但如果直接使用,它可以处理其内容

javascript - 如何防止时间与时刻相同

javascript - 如何为不使用 JQuery 动态创建的元素创建事件监听器

json - Google Apps 脚本 : Returning "null" when trying to select a specific object in a API response but I can see it in the original response

google-apps-script - 如何创建自动脚本以将Google Analytics(分析)仪表板复制到Google电子表格中

javascript - 当我运行 Node js时收到错误: SyntaxError: Unexpected token '<'

函数中的 Postgresql 公共(public)表达式表 (CTE)

PHP mysqli_fetch_array() 不会在 while 循环中将所有选择查询插入到表中