What is the best method for substituting null values for empty cells when transferring data from Google Sheets to a database?

I'm currently facing an issue with inserting spreadsheet data into a MySQL database. The problem arises when there are empty cells in the 'CUSTOMER' column, resulting in the following error message when attempting to execute the code:

"Incorrect integer value: '' for column 'CLIENTE' at row 1"

My attempt to replace empty cells with a 'null' value while preparing the query has not been successful.

I am grateful for any assistance provided.

Thank you

Spreadsheet:

function writeManyRecords() {
  
    const conn = Jdbc.getConnection(dbUrl, user, userPwd);
    conn.setAutoCommit(false);
    const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    const sheet = spreadsheet.getSheetByName('FREEZERS');
    const data = sheet.getDataRange().getValues();
    const start = new Date();
    var stmt = conn.prepareStatement('INSERT INTO FREEZERS ' +  '(PATRIMONIO,DESCRICAO,CLIENTE,LOCAL_ESTOQUE,LOCAL_ANTERIOR_ESTOQUE) values (?, ?, ?, ?, ?)');
    for (var i = 1; i < data.length; i++) {
        stmt.setString(1,data[i][0]);
        stmt.setString(2,data[i][1]);
        if(!data[i][2] === '') {
            stmt.setString(3,data[i][2]);
        }
        stmt.setNull(3,4);
        stmt.setString(4,data[i][3]);
        stmt.setString(5,data[i][4]);
        stmt.addBatch();
        Logger.log('patrimonio: '+data[i][0]+'descricao: ' + data[i][1] +' cliente: '+ data[i][2])
    }

    const batch = stmt.executeBatch();
    conn.commit();
    conn.close();

    const end = new Date();
    Logger.log('Time elapsed: %sms for %s rows.', end - start, batch.length);
  } 

Answer №1

Here is a potential solution:

if (data[i][2] === '') { stmt.setString(3,data[i][2]) }
else { stmt.setNull(3,0) } // The second number denotes the type: 0 - null, 4 - integer, and so on

Learn more about using registerOutParameter in Google Script here.

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

Error: The requested address is currently in use by Node.js and Express

Every time I try to save my files using nodemon, an error pops up with the following message: events.js:292 [0] throw er; // Unhandled 'error' event [0] ^ [0] [0] Error: listen EADDRINUSE: address already in use :::9000 [0] at Se ...

Handsontable's unique text editor feature is encountering a tricky issue with copying and pasting

In my table, there are two columns: name and code. I have developed a simple custom editor for the code column, where the user can double click on a cell to open a custom dialog with a code editor. You can view a simplified example of this functionality he ...

There seems to be an issue with the React Hooks edit form where it is not selecting the record to edit. Although the currentId is correct

I have a simple CRUD React Hooks app with an ASP.NET Core Web API. The Courses component displays a list, but when I click on a link to edit a particular course, the form shows up with empty fields. Here is the JSX for the Courses component: import React, ...

Click the link to copy it and then paste the hyperlink

I am facing an issue with copying and pasting file names as hyperlinks. I have checkboxes next to multiple files and a share button. When I check the boxes and click on the share button, I want to copy the download URLs for those files. Although I can succ ...

Exploring SQL and Relational Algebra queries across three interconnected tables - student, studies, and course

I have the following tables, with primary keys indicated in bold. student (SID, SName, semester) studies (SID, CID) course (CID, CName, CCode) Please provide SQL queries and Relational Algebra solutions for the given statements. List the names of a ...

Is there a way to cancel an ongoing AJAX request?

My website will soon have a series of jQuery AJAX calls, and I need to create a function to abort these calls. Can anyone provide guidance on how to achieve this? I have already reviewed this particular link, but it did not work for me as expected. ...

Retrieve the top X rows with the highest number of duplicated records

I am interested in finding out how to retrieve a specific number of elements with the highest frequency in a table. For instance, consider the following table: ID* | ID_USER | ID_PRODUCT | ACCESS_DATE ID is the primary key. The ID_USER may be repeated, ...

Oops, it seems like there is a TypeError with the function window.initMap in Google Maps

For the past week, I have been struggling to update my marks on Google Maps while using AJAX in an HTML page. My controller fetches data from the database and sends it back, but now I am encountering the following error: TypeError: window.initMap is not a ...

Combining multiple columns into a single output using MySQL

I am facing an issue with retrieving data from two different tables: 'card_index' and 'card_data'. The 'card_index' table structure is as follows: +-----+-----+-----+-----+ | uid | id1 | id2 | id3 | +-----+-----+-----+-----+ ...

When to safely store state data in React applications?

Imagine having a React component that updates its state based on form input. class Form extends React.Component { constructor(props) { super(props); this.handleChange = this.handleChange.bind(this); this.state = { someCheckboxState: fa ...

Using JavaScript, post an image to imgur (API version 3) from a canvas

Currently, I am working on an amusing Chrome experiment called the Mustache Mirror! If you're interested, you can check it out here. I am aiming to incorporate the Imgur API V3 for uploading an image from the canvas to Imgur and displaying the link. U ...

Click to delete the <p> element

For the past few days, I've been struggling with this issue and have tried various approaches to solve it. However, I can't seem to remove the element for some reason. Can anyone offer some insight? My objective is to add the inputted markup on ...

Unable to render ng-view due to it being enclosed within a comment block

Currently, I am in the midst of developing a single page application which employs Node, Express, and Angular. The layout of my directory follows the typical format of an Express application <app> +--public +--routes +--views +--partials ...

Adjustment in orientation compared to the position of the head-mounted display

At the moment, I am utilizing a system where the camera in the world moves based on the change in position from the hmd. The problem arises when I turn to face the positive x-axis and move forward - only the x-axis changes, making it impossible to walk for ...

How can I apply conditions to a table and then modify a record in another table simultaneously?

CREATE TABLE applicant_subjects ( id INT(10) PRIMARY KEY NOT NULL AUTO_INCREMENT, applicant_id INT(11), test_type_id INT(10), subject_id INT(10), ca_score INT(11), exam_score INT(6), score_grade VARCHAR(10), date_created VARCHAR(10), date ...

What is the best way to refresh a grid in AngularJS every 5 seconds?

I am currently utilizing angular js and I would like to auto-refresh the grid every 5 seconds. Here is my angular js code for constructing the grid: App.controller('NGTableCtrl', NGTableCtrl); function NGTableCtrl($scope, $filter, ngTableParam ...

Alter based on the RegEx JS value

I have a regular expression that looks like this: /\\.br<[0-9]+>\\/g. I am looking to replace it within the main text with the number of new lines specified between the <> in the regular expression. For example, input: Hel ...

What is the process for exporting a classifier and its parameters into a table?

I want to save the details of a classifier and its parameters in a table format. Here is the code I am using: from sklearn.tree import DecisionTreeClassifier from sklearn import datasets iris = datasets.load_iris() X, y = iris.data, iris.target clf = Dec ...

There appears to be a few bugs present in my html code

I'm uncertain about what I am missing, can someone help me out? Thank you. When I click "check," it returns wrong(); It should actually return yes(); because both have the same value and result. However, it's only giving wrong(); If the value ...

Implementing a Masonry layout within an ASP UpdatePanel

I have an ASP-WebPage where I'm using an UpdatePanel with a Dropdown and Output-Div for dynamically generated Cards. These cards are layouted by Masonry. The content of the Output-Div is bound to the Dropdown selection. Initially, the Masonry-Layout ...