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

I keep running into this frustrating error message:

missing name after . operator

This happens every time I try to run my script and it stops execution on line 10. I’ve been stuck on this for hours and can’t figure out what’s causing it. Has anyone encountered this before and found a solution?

function processDataFiles() {
  
  if(!require(lodash)) {
    install.packages("lodash"); require(lodash)}
  p_load(underscore, csv_parser)
  require(fs_utils)
  
  
  parse_and_add_metadata <- function(filepath){
    filename <- gsub(".json", "", filepath, ignore.case=T)
    parts <- strsplit(filename, "/") %>% unlist()
    dataset_name <- parts[length(parts)]
    data_obj <- parseFile(filepath)
    data_obj$dataset_type <- dataset_name
    return(data_obj)

The error always points to the same line where I’m trying to process the file path. Any help would be really appreciated!

The error arises from using R syntax within JavaScript. Google Apps Script requires valid JavaScript syntax, and the code you’ve provided contains elements from R, such as require(lodash) without quotes and the assignment operator <-. To resolve this, you should rewrite the function entirely in JavaScript. Use const _ = require('lodash') for requiring lodash, replace <- with = or const, and use JavaScript string methods like split() instead of R’s strsplit(). Ensure that you adhere strictly to JavaScript conventions to avoid syntax errors.

you’ve mixed up languages. that’s R code, not js - the google sheets script editor only runs js. the “missing name after . operator” error happens cause R syntax like <- and require(lodash) w/o quotes don’t work in js. rewrite in proper js syntax.

I see what’s wrong. You’re mixing JavaScript and R syntax, which breaks Google Apps Script since it only understands JavaScript. Your code has R-specific stuff like require(lodash) without quotes, p_load(), and the <- assignment operator. The JavaScript engine chokes on these and throws that error when it hits the dot operator. Here’s what you need to fix: Use const _ = require('lodash') if you’re in Node.js, or import lodash properly for your setup. Replace <- with normal JavaScript variable declarations. Swap out R functions like gsub() and strsplit() with JavaScript’s built-in string methods. Once you convert everything to proper JavaScript syntax, that error will go away.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.