Why did the App Script debugging process succeed, only for execution to ultimately fail?

I've been working on importing csv files into specific Google Sheets using App scripts. Although my code seems to be functioning correctly, it's not producing any output. The approach I took was creating an array for the list of csv files and another array for the sheet names in the active spreadsheet. Then, I implemented an if loop where the data from the csv file will be pushed into the corresponding sheet based on the array indexes. This logic is wrapped within an outer for loop for iteration purposes. Below is the code snippet:

function importCSVFromGoogleDrive() {

 var ss = SpreadsheetApp.getActiveSpreadsheet();// get the active spreadsheet


["Sheet1", "Sheet2", "Sheet3"].forEach(function (s) {
 var sheetArray = ss.getSheetByName(s);                   // getSheetByName


var numSheets = sheetArray.length; // save sheets in Array 


for (j = 0; j < numSheets; j++) { //loop to iterate

 var fileArray = DriveApp.getFilesByName["Acsv","Bcsv"]; //get csv files as a list 

 var file = fileArray.length; // add the list into an array

 var csvData = Utilities.parseCsv(file.getBlob().getDataAsString()); // parse csv data


 if(numSheets==csvData){  // if array Index No of numSheets equals to index of file array

    numSheets.push(csvData);// push csv data into the sheet


    numSheets.clearContents(); //clear current contents in the sheet
    numSheets.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData); // set values to the sheet
      }

   }

 })
}

The code is executing without errors, yet no data is being transferred from the csv file to the sheet. Can you shed some light on why this might be happening?

Answer №1

This paragraph discusses the incorrect syntax used for getFilesByName:

var fileArray = DriveApp.getFilesByName["Acsv","Bcsv"]; //retrieves csv files as a list 

The proper syntax should be:

DriveApp.getFilesByName(name);

In this case, name is a string. Hence, it is recommended to modify your code accordingly.

Furthermore,

var numSheets = sheetArray.length; //stores sheets in an Array 

as well as

var file = fileArray.length; //converts the list into an array

assign integers to numSheets and file, respectively. As a result, subsequent lines of code such as

numSheets.push(csvData);//adds csv data to the sheet

and

var csvData = Utilities.parseCsv(file.getBlob().getDataAsString()); //parse csv data

You may want to take some time to familiarize yourself with the Google Apps Script debugger in order to effectively debug your code.

For more information, please refer to the following link:

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

What is the best way to run a callback once several Ajax requests have finished?

I am facing a challenge with executing a callback function after multiple jQuery Ajax requests have been completed. The issue arises when these Ajax requests call another function, resulting in the functions being undefined. I suspect that the root of ...

Add a file to the input field using JavaScript and the input type="file"

I am facing the challenge of sending a file (up to a few MB) generated by JavaScript to a server using POST (not ajax POST). I am wondering how I can add a file to an input type="file" using JavaScript. After checking Pre-Populate HTML form file input, it ...

How can we dynamically resize page content to fit varying browser window sizes across different devices with the help of jQuery/JavaScript?

Looking for a solution involving an HTML form that includes a text field and a submit button. The text field is where the user enters a URL, and upon clicking the submit button, they are redirected to that URL. Seeking a way to adjust the size of the new ...

Issue with jQuery animation: Background color does not change upon scrolling

Here is my fiddle link: https://jsfiddle.net/jzhang172/owkqmtcc/5/ I am attempting to change the background color of the "content" div when scrolling anywhere within it. The goal is for the background color to change when scrolling and revert back to its ...

Tips for sending a variable to the onclick function within an HTML element

Currently, I am utilizing the tabulator library to generate tables based on json data. I am attempting to insert a button into a cell using a custom cell formatter. Below is the approach I am taking: var graphButton = function(cell, formatterParams, onRe ...

Creating a JavaScript file to incorporate into an HTML document

I stumbled upon this code snippet here This code allows me to fetch data from a php file and insert it into a div using jQuery. While the tutorial works perfectly, I'm planning to use this for about 9-10 different links and thought of consolidating a ...

Regular expressions: understanding greedy versus lazy quantifiers

Imagine this situation: a = 'one\\two\\three.txt'; The desired output is "three.txt". However, the attempted solution of: a.match(/\\(.+?)$/) is unsuccessful. What could be causing this issue? How can we successf ...

Guide to aligning the orientation of an object with a given normal vector using three.js

Imagine I have a car object where the z-rotation is positioned to face the direction it's moving in. This car is situated on an inclined ground represented by a normalized normal vector (nx, ny, nz). How can I rotate the car's x and y axes so th ...

Generating a basic PHP Request

I am a beginner in PHP and I am attempting to create a basic server using GET and POST request methods. The PHP server should simply receive JSON data and save it (POST) and then return it to the user (GET). However, for starters, I am trying this: PHP ...

The outcome of the Javascript Calculator is showing as "Undefined"

I've been attempting to create a calculator using JavaScript, but I'm facing an issue where the 'operate' function keeps returning Undefined, and I can't seem to figure out why. I suspect that the problem lies with the switch state ...

Is it possible to replicate the functionality of "npm run x" without including a "scripts" entry?

If you want to run a node command within the "context" of your installed node_modules, one way to do it is by adding an entry in the scripts field of your package.json. For example: ... "scripts": { "test": "mocha --recursive test/**/*.js --compiler ...

In jQuery, utilizing dynamic class names with variables

I have a series of images with unique classes such as: .1a .2a .3a .4a ..... I am looking to toggle some other classes named .1b .2b .3b .. and so on so that: '.1a' toggles to '1b' '.2a' toggles to &ap ...

What could be causing the Error when using the then() callback in ExpressJS following a promise?

Trying to figure out NodeJS and ExpressJS, attempting a basic Database query. db.execute('SELECT * FROM restaurant').then().catch() An error is popping up : db.execute('SELECT * FROM restaurant').then().catch() ...

Does the AngularJS inject function operate synchronously?

Does the AngularJS inject method work synchronously? Here is an example: inject(function(_$compile_, _$rootScope_) { $compile = _$compile_; rootScope = _$rootScope_.$new(); }); ...

What is the best way to incorporate interactive columns in DataTables?

I am utilizing jquery datatables to present data. <table class="report-tbl table-bordered" cellspacing="0" width="100%" id="report-tbl"> <thead> <tr> <th></th> ...

Internet Explorer IE 11 encounters an "Error: Object does not support property or method" issue

Recently, I started using the jquery circleChart.min.js plugin for creating a circle chart. It's been working perfectly on all browsers except for Internet Explorer 11 (IE11). I keep getting an error message when trying to run it in IE11. Can anyone h ...

`How can I trigger a JavaScript event when the content of an input field is modified?`

Currently, I am working on implementing validation in JavaScript. My goal is to provide the user with an alert whenever they modify the value of an input field. <input type="text" name="onchange" id="onchange" size="35" maxlength="50" /> ...

Check out the CSS media query for screens with a maximum width on larger devices

Is there a way to preview a specific div and see how it appears on mobile devices? The HTML code for the div is obtained from an external source, like this: var external_html='<div>Responsive div</div>' + '<style>@media ...

Combining a 2D array with a 3D array to create a larger 3D array with an expanded third dimension

I am facing a challenge with two arrays in my coding project. Array A represents 3124 models with 5 reference parameters, while array B represents 3124 models, 19 time steps per model, and 12288 temperature field data points per time step. My goal is to a ...

Quickest method for comparing two lists against each other

If I need to determine whether a string value from list1 is present in list2, what would be the most efficient approach? Here is an example of Pseudo Code: StringList List1 = {"a", "b", "c", "d"}; StringList List2 = {"d", "c", "b", "a"}; foreach (string ...