简体   繁体   中英

Google Apps Script: TypeError: undefined is not a function

I'm trying to get this demo Google Sheets Blog Demo to run. I followed all steps but got this error after running https://script.google.com/macros/s/[spreadsheetID]/exec?key=abcdef :

TypeError: undefined is not a function (line 19, file "Code")

Line 19 = var headings = rows[0].map(String.toLowerCase); Below is the related function and here the full code .

function doGet(e) {
  if (!isAuthorized(e)) {
    return buildErrorResponse('not authorized');
  }

  var options = {
    page: getPageParam(e),
    category: getCategoryParam(e)
  }

  var spreadsheet = SpreadsheetApp.openById(SPREADSHEET_ID);
  var worksheet = spreadsheet.getSheets()[0];
  var rows = worksheet.getDataRange().sort({column: 2, ascending: false}).getValues();

  var headings = rows[0].map(String.toLowerCase);
  var posts = rows.slice(1);

  var postsWithHeadings = addHeadings(posts, headings);
  var postsPublic = removeDrafts(postsWithHeadings);
  var postsFiltered = filter(postsPublic, options.category);

  var paginated = paginate(postsFiltered, options.page);

  return buildSuccessResponse(paginated.posts, paginated.pages);
}

How to fix this issue?

When I saw the full script at the GitHub, it was found that that script had published 2 years ago. In this case, V8 had not been added. In order to avoid the error of TypeError: undefined is not a function (line 19, file "Code") , how about the following 2 patterns?

Pattern 1:

In this pattern, the script is used without V8 runtime. In this case, please disable V8 runtime at the script editor. By this, var headings = rows[0].map(String.toLowerCase); can be used without the error.

Pattern 2:

In this pattern, the script is used with V8 runtime. In this case, please enable V8 runtime at the script editor. And also, please modify the script as follows.

From:
 var headings = rows[0].map(String.toLowerCase);
To:
 var headings = rows[0].map(Function.prototype.call, String.prototype.toLowerCase);

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM