1

I am trying to put a comment using NODEJS and the "Google Spread-Sheet" module, I have managed to write in cells, read, delete, but I can not put a comment in a cell, and the tutorials I see only teach discontinued forms, someone could help me? thank you very much.

const { GoogleSpreadsheet } = require('google-spreadsheet');
const creds = require('./googleSheetCredentials.json');

async function accessSpreadSheet() {
    const doc = new GoogleSpreadsheet('XXXXXXXXXXXX');
    await doc.useServiceAccountAuth(creds);
    await doc.loadInfo();
    const sheet = doc.sheetsByIndex[0];

    await sheet.addRow({
        Login: 'hello world', // column A
        Date: '2020-01-01' // column B
    });

    const row = await sheet.getRows();
    console.log(row);

    //await sheet.addComment('2', 'Login', 'Hello world'); Doesn't work 
}

accessSpreadSheet();

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Welcome to stack. Can you edit your question and add a link to the method in the API documentation where it shows you can add comments to cells. – Linda Lawton - DaImTo Apr 20 '22 at 08:05
  • 1
    You can't attach a comment to a cell. Comments are linked to the drive (notes are attached to cells) – Mike Steelson Apr 20 '22 at 08:07
  • @MikeSteelson Okay, it's my bad, you know where is the official documentation? or how can put a note into a cell?, thanks :=) –  Apr 20 '22 at 09:15
  • You can refer to https://developers.google.com/apps-script/reference/spreadsheet/range#setnotenote with gas. But I don't know how to integrate with node.js – Mike Steelson Apr 20 '22 at 09:30
  • In your script, if you want to insert a note to a cell, what do you want to do at `await sheet.addComment('2', 'Login', 'Hello world');`? Do you want to put a note of `Hello world` to the column "A" of the added row? First, I thought that I would like to correctly understand your goal. I apologize for this. – Tanaike Apr 20 '22 at 12:41
  • By guessing your goal, I proposed a modified script as an answer. Could you please confirm it? If I misunderstood your goal and that was not useful, I apologize. – Tanaike Apr 20 '22 at 13:25

1 Answers1

0

I believe your goal is as follows.

  • You want to insert a note to a cell.
    • From your showing script and await sheet.addComment('2', 'Login', 'Hello world');, I guessed that you wanted to insert a note to the column "A" of the added row.
  • You want to achieve this using node-google-spreadsheet.

In this case, how about the following modification?

Modified script:

const { GoogleSpreadsheet } = require('google-spreadsheet');
const creds = require('./googleSheetCredentials.json');

async function accessSpreadSheet() {
  const doc = new GoogleSpreadsheet('XXXXXXXXXXXX');
  await doc.useServiceAccountAuth(creds);
  await doc.loadInfo();
  const sheet = doc.sheetsByIndex[0];

  // I modified below script.
  const res = await sheet.addRow({
    Login: "hello world", // column A
    Date: "2020-01-01", // column B
  });
  await sheet.loadCells();
  sheet.getCellByA1("A" + res.rowIndex).note = "sample note";
  await sheet.saveUpdatedCells();
}

accessSpreadSheet();
  • When you run this script, a note of sample note is inserted into the column "A" of the added row.

Note:

  • This modified script supposes that you have already been able to get and put values to Google Spreadsheet using Sheets API with google-spreadsheet and your script of await sheet.addRow works. Please be careful about this.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165