node-google-spreadsheet icon indicating copy to clipboard operation
node-google-spreadsheet copied to clipboard

Retrieving a row and adding it into another character sheet tab does not carry its properties.

Open NewCoderOnTheBlock opened this issue 2 years ago • 3 comments

Hello everyone, so here's my code

let rows: GoogleSpreadsheetRow = getSomeRows()
let row = rows.filter(...)
let index = (await this.memo.addRow(row)).rowIndex
addborders(this.memo, index, 0)

You can see the task are simple, Retrieve a row, store it into the variable and finaly add all the information at the end of another google sheet tab with the help of .addRow(). What I was expecting is for it to keep all its cells properties but what I got is a row added in RAW meaning there are no borders, padding etc so I resolve this by adding them manually with my small function I created called addborders.

The code is working perfectly fine but the only thing I couldn't find is how to retrieve the cell history.

If anyone knows how to do it, would be very much appreciated & maybe it could carry the formatting properties too so i don't have to use my addborders function anymore while using .addRow().

Best Regards.

NewCoderOnTheBlock avatar Sep 01 '22 23:09 NewCoderOnTheBlock

the row based API uses the spreadsheets.values api which only includes values.

If you want to deal with formatting, you'll have to use the cell-based methods, which are unfortunately a bit clunkier to use.

I'd have to do some major rejigging to get the row-based methods to work this way.

theoephraim avatar Jun 26 '23 18:06 theoephraim

probably what would make more sense in this case is to use the row-based methods to find the location of the data you are looking for, and then use copy paste request to copy the raw cells.

Although this method is not yet supported - it shouldn't be too tough to add.

theoephraim avatar Jun 29 '23 03:06 theoephraim

Hey @theoephraim , thanks for your reply since I posted this question and Question #583. I have built an alternative. It may not be perfect, but it gets the job done. If you are interested, I will share the code I have written.

GoogleSpreadsheetWorksheet.prototype.moveRows = async function (movedRows: GoogleSpreadsheetRow[], destination: GoogleSpreadsheetWorksheet): Promise<void> {
    let columnRange: any = movedRows[0].a1Range, i = 0, lastColumn = "", responseRanges = [`${this.a1SheetName}`];
    // Extract last column
    while (columnRange[i] != ":") i++; i++
    while (isNaN(columnRange[i])) lastColumn += columnRange[i], i++
    await this.loadCells(`A${movedRows[movedRows.length - 1].rowIndex}:${lastColumn}${movedRows[0].rowIndex}`) //load the only cell that interest us
    //console.log((this as any).lastColumnLetter)
    const appendRequests = {
        appendCells: {
            sheetId: destination.sheetId,
            fields: "*",
            rows: movedRows.map(row => {
                return {
                    values: row._rawData.map((data: string, index: number) => {
                        return {
                            userEnteredValue: {
                                stringValue: data
                            },
                            userEnteredFormat: this.getCell(row.rowIndex - 1, index).userEnteredFormat
                        }
                    })
                }
            })
        },
    };

    const deleteRequests: any[] = movedRows.slice(0).reverse().map(row => {
        return {
            deleteDimension: {
                range: {
                    sheetId: this.sheetId,
                    dimension: "ROWS",
                    startIndex: row.rowIndex - 1,
                    endIndex: row.rowIndex,
                },
            },
        };
    });

    const requests = deleteRequests.concat([appendRequests]);
    await (this as any)._spreadsheet.axios.post(':batchUpdate', {
        requests,
        includeSpreadsheetInResponse: true,
        ...responseRanges && {
            responseIncludeGridData: true,
            ...responseRanges.join() !== '*' && { responseRanges },
        },
    });

    //await (this as any)._spreadsheet._makeBatchUpdateRequest(requests, responseRanges)
}

GoogleSpreadsheetRow.prototype.moveRow = async function (destination: GoogleSpreadsheetWorksheet): Promise<void> {
    await this._sheet.moveRows([this], destination)
};

NewCoderOnTheBlock avatar Jul 14 '23 21:07 NewCoderOnTheBlock