Having trouble with implementing forEach in Google Script

Hey there, I'm having a syntax error in my GoogleScript. This is actually my first script ever, so I'm a bit lost as to why it's not working. The main goal is to extract data from a Google sheet and use it to create labels based on a document template and then store them in a specific folder.

function readSheet() {
    
    const labelFolder = DriveApp.getFolderById("1NTnJgjCewcFcrtMsl5DtRgrvFUNCdsvt"); 
    
    const docFile = DriveApp.getFileById("1pRy9BLDuDka7-9PKnHGgO1AtgpoWIY45t7ypVhXXM8I");
    
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("test");
    
    const data = sheet.getRange(2, 1, sheet.getLastRow() - 1, 10).getValues();
    
    data.forEach(row => {
    
        shippingLabels(row[5], row[6], row[8], docFile, labelFolder);
    });
}
    
function shippingLabels(user, address, package, docFile, labelFolder) {
       
    const labelFile = docFile.makeCopy(labelFolder);
    
    const tempDocFile = DocumentApp.openById(labelFile.getId());
    
    const body = tempDocFile.getBody();
    
    body.replaceText("{user}", user);
    
    body.replaceText("{address}", address);
    
    body.replaceText("{package}", package);
    
    tempDocFile.setName(user);
    
    tempDocFile.saveAndClose(); 
}   

Thanks!

Answer №1

Please make the following changes and test it again.

From:

const sheet = SpreadsheetApp.gettActiveSpreadSheet().getSheetByName("test");

To:

const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("test");
  • In this scenario, gettActiveSpreadSheet() should be changed to getActiveSpreadsheet().
  • Once you have made the above modification in your script, please ensure that the folder ID, Google Document ID, and values from Google Spreadsheet are accurate to avoid any errors. If you encounter any other issues, could you please provide detailed information about them? I would like to investigate further.

Reference:

Answer №2

I made a modification in the forEach loop like so:

data.forEach(function(row) {
      shippinglabels(row [5],row [6],row [8],docFile,labelFolder);
});

This change was all I needed to get it working.

Appreciate the assistance!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attaching a function to the "onselect" event that manages the character limit within an input text field

I have been developing a function to restrict the number of characters a user can enter into a text field. Here's what I have so far: $.fn.restringLength = function (id, maxLen) { $(id).keypress(function(e){ var kCode = ...

In my Laravel Vue JS project, every Put and Delete route method triggers a 302 error

I'm encountering an issue with my Laravel Vue JS application where the PUT and DELETE methods are throwing a 302 error. Here is the response from my localhost: And this is the response from my server: While everything works perfectly on my lo ...

Switching React Icons when Clicked

I'm struggling to understand this. I want the React icons below to be filled and remain filled when clicked, changing back to outlined when another is clicked. Here's the code: import { useState } from "react"; import { Link } from "react-router- ...

ASP.NET "Data" Error: Trouble Parsing JSON Data on the Front-End

I am currently facing an issue with the configurations on my asmx page. The code is set up like this: using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.Script.Serialization; using Syst ...

Phonegap application functioning smoothly on computer, encountering issues on mobile device

Hey there! I recently developed a phonegap app that retrieves JSON data from a YQL link and presents it to the user. It works perfectly on Google Chrome desktop, but my client mentioned that it doesn't work on his Android 2.3 device. Could you help me ...

Generating an associative array within a loop

Good day! I have successfully created an array using a for loop. Initially, the array is empty until it goes through the loop. Now, I am looking to include an associative array in my existing setup. Currently, this is how my array looks: [0] => Array ...

Issue with modal component triggering unexpected page reload

I'm encountering a strange issue with my modal in Vue.js. It only appears on a specific page named 'Item', but when I click on a different view, the page reloads unexpectedly. This problem seems to occur only with the route containing the mo ...

RegEx for constraining string length

I am attempting to identify all instances where the length of a string is more than 1. This is the regex I am using: string.match(/\B\w+(?=\w)/gi); I also attempted this approach: string.match(/^\B\w+(?=\w){2,}$/gi); Unfor ...

Switch between Accordions/Tabs with JavaScript (ES6)

Encountering a minor issue with the accordion/tab layout provided in this link: https://jsfiddle.net/drj3m5tg/ The problem is that on mobile, the "+" icon remains as "-" when opening and closing tabs. Also, in desktop view, sometimes tabs need to be clic ...

Load page content dynamically with Ajax in a specific div while still allowing the option to open the content in a new tab by right-clicking

As I work on developing a new website that utilizes a MySQL database to sort various items into categories and subcategories, I have implemented a script that dynamically loads category content into a div without requiring a page reload. This seamless load ...

Assistance required in translating Firebase syntax from version 7.15.1 to version 9.6.1

I'm embarking on my Firebase journey and trying to follow a tutorial that seems to be a bit outdated. I could use some assistance in updating the code to match the newer version, as it appears the syntax has changed. The tutorial uses Firebase 7.15.1, ...

How can you check if a field is blank without allowing any empty spaces to be saved in JavaScript?

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <html> <body> <form id="api_crud_form" name="api_crud_form" action="https://www.google.es"> Name: <input type="text" name="name" id=" ...

Modify the contents of three div elements by incorporating JavaScript and ajax techniques

Currently working with rails 4 and JavaScript, I am faced with the following dilemma: I want to be able to alter the content of 3 specific divs on my page by clicking a single button. The values in these divs are being created by a Ruby function located in ...

Reactive forms in Angular now support changing focus when the Enter key is pressed

I have successfully created a table and a button that generates dynamic rows with inputs inside the table. One issue I'm facing is that when I press enter in the first input, a new row is created (which works), but I can't seem to focus on the ne ...

"Switching Classes with a Click Event: A Step-by-

This script is designed to work with SoundCloud's widget in order to enable remote text buttons. I am attempting to modify it so that it functions as a remote for an image button that toggles between different pictures for pause and play, rather than ...

Default Selection Issue with Radio Buttons in AngularJS

I am currently encountering an issue with the code snippet included in my directive template '<li ng-repeat="f in foos">' + '<input type="radio" ng-change="foo(f.key)" ng-model="selectedFoo" name="foos" id="{{f.key}}" value="{{f.ke ...

Error 500: Issue with JQuery AJAX File Upload

Hey there, I'm facing an issue with doing a file upload using JQuery's AJAX feature as I keep getting the error 500. $(function() { $( 'form' ).submit ( function() { $.ajax({ type: &a ...

Is there a way to automatically advance to the next question in Surveyjs without needing to click the Continue

I'm currently integrating SurveyJs into my web application. The first 3 pages of my survey contain HTML elements that I would like to automatically slide after a few seconds, similar to the functionality on the following sample site using SurveyJS: O ...

Guide on updating a single element in a Firebase array

I have an array stored in my firebase database, structured like this: matches:[ {match:{id:1,data:...}}] I am looking for a way to update just one specific item within this array. Let's say I want to locate the match with the ID of 32 and modify its ...

Inheritance from WebElement in WebdriverIO: A Beginner's Guide

I am seeking a solution to extend the functionality of the WebElement object returned by webdriverio, without resorting to monkey-patching and with TypeScript type support for autocompletion. Is it possible to achieve this in any way? class CustomCheckb ...