Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将表单数据从我的Chrome扩展写入Google工作表_Javascript_Google Chrome Extension_Google Sheets Api - Fatal编程技术网

Javascript 将表单数据从我的Chrome扩展写入Google工作表

Javascript 将表单数据从我的Chrome扩展写入Google工作表,javascript,google-chrome-extension,google-sheets-api,Javascript,Google Chrome Extension,Google Sheets Api,更新了片段和今天的进度: 我正在编写一个Chrome扩展,它本质上是一个带有表单的弹出窗口,我想将输入表单的数据写入Google表单。目前,我的扩展由manifest.json、弹出脚本和后台脚本组成 manifest.json(相关部分): “背景”:{ “脚本”:[“background.js”], “持续”:假 }, “content_脚本”:[{“js”:[“content.js”],“matches”:[“”]}], “权限”:[ “标签”, “存储”, "", “身份”, “htt

更新了片段和今天的进度:

我正在编写一个Chrome扩展,它本质上是一个带有表单的弹出窗口,我想将输入表单的数据写入Google表单。目前,我的扩展由manifest.json、弹出脚本和后台脚本组成

manifest.json(相关部分):

“背景”:{
“脚本”:[“background.js”],
“持续”:假
},
“content_脚本”:[{“js”:[“content.js”],“matches”:[“”]}],
“权限”:[
“标签”,
“存储”,
"",
“身份”,
“https://*.googleapis.com/*”

]
使用谷歌的API向电子表格中写入内容是一种“推”而非“贴”

我使用chrome.identity.getAuthToken成功地实现了这一点,然后使用以下命令运行获取:

    chrome.identity.getAuthToken({interactive: true}, function(token) {
        var params = {
            'values': [
                ['Row 1 Col A','Row 1 Col B'],
                ['Row 2 Col A','Row 2 Col B'],
            ]
        };
        let init = {
            method: 'PUT',
            async: true,
            body: JSON.stringify(params),
            headers: {
                Authorization: 'Bearer ' + token,
                Content-Type': 'application/json'
            },
            contentType: 'json',
        };
        fetch('https://sheets.googleapis.com/v4/spreadsheets/***YOUR SHEET ID****/values/****YOUR RANGE*****?valueInputOption=USER_ENTERED&key=***YOUR API KEY***', init)
            .then((response) => response.json())
            .then(function(data) {
                //console.log(data);
                //Returns spreadsheet ID, update tange, cols and rows
            });
        })
   });
所有这些都在后台脚本中,我将第1行Col A等作为值,这将是范围的第一个单元格


希望有帮助。

小心!如果要追加数据,
查询参数位于
:append
之后

fetch(`https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/${range}:append?valueInputOption=${valueInputOption}`, init)

你能发布相关的代码片段吗?我正在寻找从google sheet到chrome extension的数据。如果我不想在后台脚本中使用它,我将如何使用它?我至少有6个月没有做过扩展,所以我的记忆有点粗略,但我想你在哪里使用JS取决于你希望它如何工作。只要您在清单权限中具有标识。如果您正在根据popup.html/js中的用户输入写入电子表格,则将其放在那里,因为它仅在用户提交时才会运行。