Search code examples
node.jsgoogle-sheetsgoogle-apigoogle-sheets-api

NPM "google-spreadsheet" formatting a value added by .addRows()


I'm using the NPM "google-spreadsheet" on a node.js project.

When I add some rows with the .addRows() command, sometimes the format is converted by Google sheet (like phone number). I would like to add the values with the format I wanted ;)

This is an extract of my code.

I would like to write in the script that the content of the column 'phone_number' should be pure text.

const { GoogleSpreadsheet } = require('google-spreadsheet');
const GS_creds= require('../xxxxxx.json');
const GS_concertannuel = new GoogleSpreadsheet('yyyyyyyyy');
await GS_concertannuel.useServiceAccountAuth(GS_creds);
await GS_concertannuel.loadInfo();
const GS_CA_tab_hebergements = GS_concertannuel.sheetsByTitle['hebergements'];
const ajout_gs_hebergements = GS_CA_tab_hebergements.addRows([{
                'identifiant': myIdentifiant,
                'phone_number': myPhoneNumber // 0909090909
            }]);

THIS IS THE INPUT AND OUTPUT :

INPUT : The phone number I have in the Database is 0909090909.

OUTPUT : The phone number copied in the Google Sheet with google-spreadsheet is 909090909 (without the first 0). The OUTPUT I would like is 0909090909, that means in string format and not number.

I read the documentation, but I don't understand. (https://theoephraim.github.io/node-google-spreadsheet/#/).

Despite the advertised "The most popular Google Sheets API wrapper for javascript / typescript" text, I don't find a lot of information on Internet.

I could try IA ;) Well I start by pair's help ;)

Thank you guys


Solution

  • In your situation, as a simple modification, how about the following modification?

    From:

    const ajout_gs_hebergements = GS_CA_tab_hebergements.addRows([{
                    'identifiant': myIdentifiant,
                    'phone_number': myPhoneNumber // 0909090909
                }]);
    

    To:

    const ajout_gs_hebergements = GS_CA_tab_hebergements.addRows(
       [
         {
           identifiant: myIdentifiant,
           phone_number: myPhoneNumber, // 0909090909
         },
       ],
       { raw: true }
     );
    

    or

    const ajout_gs_hebergements = GS_CA_tab_hebergements.addRows(
       [
         {
           identifiant: myIdentifiant,
           phone_number: "'" + myPhoneNumber, // 0909090909
         },
       ]
     );
    
    • I thought that the reason for your current issue is due to By this modification, the value is put as a text.

    Reference: