如何在现有SpreadSheet中添加新表格?带有Node.js的Google Sheets API

时间:2019-02-22 03:26:08

标签: node.js google-sheets-api

使用针对 Node.js 的Google-Sheets-API,如何以编程方式在现有电子表格中添加/创建新表格?

enter image description here

4 个答案:

答案 0 :(得分:4)

实际上是可能的,并记录在Spreadsheets/batchUpdate/Requests#AddSheetRequest

authorize(JSON.parse(clientSecretContent), (auth) => {
    const sheets = google.sheets({ version: 'v4', auth });
    const request = {
        // The ID of the spreadsheet
        "spreadsheetId": spreadsheetId,
        "resource": {
            "requests": [{
               "addSheet": {
                    // Add properties for the new sheet
                    "properties": {
                        // "sheetId": number,
                        // "title": string,
                        // "index": number,
                        // "sheetType": enum(SheetType),
                        // "gridProperties": {
                        //     object(GridProperties)
                        // },
                        // "hidden": boolean,
                        // "tabColor": {
                        //     object(Color)
                        // },
                        // "rightToLeft": boolean
                    }
                }
            ]}
        }
    };

    sheets.spreadsheets.batchUpdate(request, (err, response) => {
        if (err) {
            // TODO: Handle error
        } else {
            // TODO: Handle success
        }
    });
});

答案 1 :(得分:0)

Google Sheets API docs似乎没有指定创建新表格的方法。您可以在工作表上执行的唯一操作是copyTo method,它将一个电子表格的工作表复制到另一个电子表格中。

如果确实存在,则可能会通过对/ v4 / spreadsheets / {spreadsheetId} / sheets的POST请求来完成,但同样,它没有记录在案,如果尝试过它可能会行不通。

答案 2 :(得分:0)

使用batchUpdate和addSheet是可能的 https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchUpdate

示例:

const sheetname = 'my sheetname'
const client = new google.auth.JWT(
    client_email,
    null,
    private_key,
    ['https://www.googleapis.com/auth/spreadsheets'],
    null
)

const jwt = await new Promise((resolve, reject) => {
    client.authorize((err, tokens) => {
        if (err) {
            reject(err);
        } else {
            google.options({
                auth: client
            });
            resolve();
        }
    });
});

const sheets = await google.sheets('v4');

try {
    const request = {
        spreadsheetId: spreadsheetId,  
        resource: {
            requests: [{
                addSheet: {
                    properties: {
                        title: sheetname
                    }
                }
            }],  
        },
        auth: client,
    };

    const resp = await sheets.spreadsheets.batchUpdate(request)
} catch (error) {
    console.log(error)
}

答案 3 :(得分:0)

对于后代来说,这是一个基于承诺的(异步/等待)Sheets API示例,说明如何向电子表格中添加工作表(标签)。

await api.spreadsheets.batchUpdate ({ 
          spreadsheetId: spreadsheetId, 
          resource: {requests: [ {addSheet: {properties: {title: tabName }}}]}});

完整示例

async function addSheet (auth, spreadsheetId, tabName) {
    const api = google.sheets({version: 'v4', auth: auth});
    try {
      // Only add sheet if it doesn't already exist
      if ((await api.spreadsheets.get({spreadsheetId: spreadsheetId})).data.sheets
        .filter(sheet => sheet.properties.title === tabName).length === 0) {
        await api.spreadsheets.batchUpdate ({ 
          spreadsheetId: spreadsheetId, 
          resource: {requests: [ {addSheet: {properties: {title: tabName }}}]}});
      }
    } catch (err) {
      console.log('Sheets API Error: ' + err);
    }
}