JavaScript "missing name after . operator" error in Google Sheets

I’m facing an error message while working on my Google Sheets code:

missing name after . operator

This appears on line 10 during my debugging sessions. Has anyone experienced this issue and can suggest a fix?

function manageCSVFiles() {
  
  if(!require(packageHandler)) {
    install.packages("packageHandler"); require(packageHandler)}
  p_load(pipeTools, dataManagement)
  require(dataHandler)
  
  
  readCSVWithHeader <- function(filepath){
    fileBaseName <- gsub(".csv", "", filepath, ignore.case=T)
    segments <- strsplit(fileBaseName, "/") %>% unlist()
    series <- segments[length(segments)]
    fileData <- fread(filepath)
    fileData$source_series <- series
    return(fileData)

I’m unsure about the cause of this syntax error. The code appears correct, but Google Sheets keeps showing this error. Any suggestions for resolving this issue would be greatly appreciated.

You wrote R code but you’re trying to run it in Google Apps Script. I made this exact mistake when I started automating Google Sheets - the syntax looks similar but they’re totally different languages. Google Apps Script only runs JavaScript, so fread(), the %>% pipe operator, and require() don’t work at all. You need to rewrite this in JavaScript using Google’s built-in tools. For CSV files, use DriveApp.getFileById() to grab files and Utilities.parseCsv() to parse them. That dot operator error? JavaScript is choking on your R syntax because it can’t find valid properties after the dots in your R function calls.

You’re mixing R syntax with Google Apps Script, which only runs JavaScript. The <- operator, require() calls, and %>% pipes are all R-specific - JavaScript can’t understand them. Apps Script needs JavaScript syntax like var, let, or const for variables and = for assignments. You’ll have to rewrite this whole function in JavaScript. Use JavaScript’s built-in methods or Google’s DriveApp and Utilities services for CSV processing instead of R packages.

hey claire, you’re mixing R code with javascript there. google sheets uses javascript for apps script, not R syntax. that <- assignment and require() stuff won’t work. use = for assignments and different import methods for js libraries.