简体   繁体   English

按名称打开电子表格;如果找不到,则创建新的电子表格

[英]Open spreadsheet by name or create new spreadsheet if none is found

I'm using the Google sheets API and Nodejs to append values to a spreadsheet. 我正在使用Google表格APINodejs将值附加到电子表格。 However, i would like to get the spreadsheet ID by the spreadsheet's name, or create a new spreadsheet if none is found. 但是,我想通过电子表格的名称获取电子表格ID,如果找不到,则创建一个新的电子表格。 To be clear, what i mean is what IFTTT does with it's Append to Spreadsheet action. 明确地说,我的意思是IFTTT的“附加到电子表格”操作。 I cannot know the spreadsheet ID beforehand. 我无法事先知道电子表格ID。

在IFTTT中按名称打开电子表格

Assuming you already know how to authenticate with googleapis , here is a minimal example how to query the Drive API to search for spreadsheets with a specific name and using the Sheets API to create if there is none. 假设您已经知道如何使用googleapis进行身份验证,这是一个最小的示例,该示例如何查询Drive API以搜索具有特定名称的电子表格,以及如何使用Sheets API创建电子表格。

// auth is google.auth.OAuth2()
const sheets = google.sheets({version: 'v4', auth})
const drive = google.drive({version: 'v3', auth})

var fileName = 'yourSpreadSheetName'
var mimeType = 'application/vnd.google-apps.spreadsheet'

drive.files.list({
    q: `mimeType='${mimeType}' and name='${fileName}'`,
    fields: 'files(id, name)'
}, (err, res) => {
    if (err) return console.log('drive files error: ' + err)
    const files = res.data.files
    if (files.length) {

        // There is an existing spreadsheet(s) with the same filename
        // The spreadsheet Id will be in files[x].id
        console.log(`found spreadsheet with id: ${files[0].id}`)

    } else {

        // Create spreadsheet with filename ${fileName}
        sheets.spreadsheets.create({
            resource: {
                properties: { title: fileName }},
            fields: 'spreadsheetId'
        }, (err, spreadsheet) => {
            if (err) return console.log('spreadsheets create error: ' + err)
            // The spreadsheet Id will be in ${spreadsheet.data.spreadsheetId}
            console.log(`created spreadsheet with id: ${spreadsheet.data.spreadsheetId}`)
        })
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建新的电子表格,以表格 Google Scripts 的名称命名 - Create new spreadsheet name it by the name of sheet Google Scripts 如何制作电子表格复印机的循环(将整个电子表格复制到具有新名称的新电子表格中) - How to make a loop of an Spreadsheet copier (Copies an entire Spreadsheet into a new one with a new name) 将工作表复制到新电子表格,将单元格值添加到新电子表格名称的末尾 - Copy sheet to new spreadsheet, Add cell value to end of new spreadsheet name 范围未找到 - Google电子表格 - Range not found - Google Spreadsheet 开源电子表格解决方案 - Open source Spreadsheet Solution 如何在 Node.js 中使用 googleapis 创建新的 Google 电子表格 - How to Create New Google Spreadsheet Using googleapis in Node.js Google Sheets / Apps Script:在特定文件夹中创建新电子表格的功能 - Google Sheets / Apps Script: Function to create a new spreadsheet in specific folder 通过GDrive REST API用Javascript创建新的电子表格? - Create new spreadsheet via GDrive REST API in Javascript? 如何将命名的电子表格创建到文件夹中 - How to create named Spreadsheet into a folder 代码通过它的名称获取电子表格URL并粘贴到另一个电子表格中 - Code to get Spreadsheet URL through it's name and paste in another spreadsheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM