Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

node.js - Firestore to Google Sheets with a Cloud Function

I am building an appointments app and I would like to keep track of Firestore updates in a google sheet.

Basically, what the function does, is to listen for updates in firestore, then loop through every object in the document and compare date (doc id) and time values with a concatenation of date and time columns from the spreadsheet to paste object data in the corresponding row.

For some reason when a doc is updated, my cloud function is not updating the spreadsheet

This is the Cloud function:

exports.writeToSheetTrigger = functions.firestore
    .document("citas/{date}")
    .onUpdate(async (change, context) => {
        var values = change.after.data();

        const date = context.params.date;

        try {
            const spreadsheet = functions.config().googleapis.sheet;
            
            google.auth
                .getClient({
                    
                    scopes: ["https://www.googleapis.com/auth/spreadsheets"],
                })
                .then(async (auth) => {
                    const api = google.sheets({ version: "v4", auth });
                    const getSheets = api.spreadsheets;
                    const sheetVals = await getSheets.values.get({
                        spreadsheetId: "SOME ID",
                        range: `Hoja 1!C2:D1000`,
                    });

                    // create a concatenated column with date and time
                    let dataArray = sheetVals.data.values;

                    let concat = dataArray.map((r) => {
                        r.push(r[0] + " " + r[1]);
                        return r;
                    });

                    for (time of values) {
                        const rowData = [
                            [
                                values[time].ocuppied,
                                values[time].service,
                                values[time].name,
                                values[time].phone,
                                values[time].email,
                            ],
                        ];

                        let rowToReplace;

                        for (let i = 0; i < concat.length; i++) {
                            if (concat[i][2] === `${date} ${values[time].hour}`) {
                                rowToReplace = i + 2;
                            }
                        }

                        const theRange = `Hoja 1!E${rowToReplace}:I${rowToReplace}`;

                        const payload = {
                            spreadsheetId: "SOME ID",
                            range: theRange, //e.g. 'Sheet1!A2:D',
                            valueInputOption: "USER_ENTERED",
                            resource: {
                                values: rowData,
                            },
                        };

                        let res = [];

                        res.push(getSheets.values.update(payload));
                    }

                    return await Promise.all(res);
                })

                .catch((err) => {
                    console.warn("error writing to spreadsheet", err);
                    return err;
                });
        } catch (err) {
            console.error(err);
            await err;
        }
    }); 

This is how the database and spreadsheet look like :

spreadsheet database

Many thanks!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...