我有 电子表格 与问卷调查的链接。我想知道如何从每个问卷中获取问题。
我想我要做的是:最好的情况是使用脚本编辑器,并在行上迭代,最坏的情况是做webscrapping。
const puppeteer = require('puppeteer');
function appendString() {
var range = SpreadsheetApp.getActiveSheet().getActiveRange();
var numRows = range.getNumRows();
var numCols = 0;
for (var i = 1; i <= numRows; i++) {
for (var j = 1; j <= numCols; j++) {
var currentValue = range.getCell(i,j).getValue();
await page.goto(currentValue);
const pollFrame = page.frames().find() # From there I have some difficulties
}
}
}
但我得到了以下错误。
SyntaxError: await is only valid in async function (ligne 10, fichier "Code.gs")
更不用说异步问题和按钮了,我还得点击,选择是这样的:
<div class="freebirdFormviewerViewItemsItemItemTitle exportItemTitle freebirdCustomFont" id="i1" role="heading" aria-level="3" aria-describedby="i.desc.310938276">How often did you fly before the Covid-19 epidemic? </div>
但是ID没有按照逻辑数字顺序排列,所以我不知道如何自动提取它们。
那我就不知道怎么做了。不知道是不是因为是同一个供应商的产品,所以比较简单。
下面是相当于csv格式的。
https://docs.google.com/forms/d/e/1FAIpQLSfzocEm6IEDKVzVGOlg8ijysWZyAvQur0NheJb_I_xozgKusA/viewform?usp=sf_link
https://docs.google.com/forms/d/e/1FAIpQLScrm0ZTrvlONf5MX37N93H_FajNzfbNy9ZtitX-Vq9PPuLPHA/viewform?usp=sf_link
https://docs.google.com/forms/d/e/1FAIpQLSeolFSh3OyS_XpX1lRIJP-8CH8WG0X0hL98SM9d85LqC22Bow/viewform?usp=sf_link
所以我尝试了Neven Subotic的好心发布的答案。
// this array will store forms and their questions
let formAndQuestions = [];
let formIds = ["https://docs.google.com/forms/d/e/1FAIpQLSfzocEm6IEDKVzVGOlg8ijysWZyAvQur0NheJb_I_xozgKusA/viewform?usp=sf_link",
"https://docs.google.com/forms/d/e/1FAIpQLScrm0ZTrvlONf5MX37N93H_FajNzfbNy9ZtitX-Vq9PPuLPHA/viewform?usp=sf_link",
"https://docs.google.com/forms/d/e/1FAIpQLSeolFSh3OyS_XpX1lRIJP-8CH8WG0X0hL98SM9d85LqC22Bow/viewform?usp=sf_link"]
formIds.forEach( formId => {
const form = FormApp.openById( formId );
// lets get the name
const formName = form.getTitle();
// first we get all items
const allItemsInThisForm = form.getItems();
// then we get filter out anything that is not a questions
const allQuestionsInThisForm = allItemsInThisForm.filter( item => {
return isThisItemAQuestion( item )
});
// now we store them in our object
formAndQuestions.push( {
formId: formId,
formName: formName,
questions: allQuestionsInThisForm
})
});
// this function is used to only get the itemTypes you want
// see reference for more information
function isThisItemAQuestion( item ){
const itemType = item.getType();
const validQuestionItemTypes = [ FormApp.ItemType.TEXT, "add others here" ]
let isValid = false;
validQuestionItemsTypes.forEach( validItemType => {
if( itemType == validItemType ) {
isValid = true;
}
});
return isValid
}
不幸的是,我得到了以下错误信息,内容如下: Exception: No item with the given ID could be found, or you do not have permission to access it. (line 9, "const form = FormApp.openById( formId );")
. 我不明白。正如你在gif中看到的,我可以打开这些链接,所以我应该有权限访问它们,不是吗?
我也试过Ruben的想法与。
// this array will store forms and their questions
let formAndQuestions = [];
let formIds = ["https://docs.google.com/forms/d/e/1FAIpQLSfzocEm6IEDKVzVGOlg8ijysWZyAvQur0NheJb_I_xozgKusA/viewform?usp=sf_link"]//,
//"https://docs.google.com/forms/d/e/1FAIpQLScrm0ZTrvlONf5MX37N93H_FajNzfbNy9ZtitX-Vq9PPuLPHA/viewform?usp=sf_link",
//"https://docs.google.com/forms/d/e/1FAIpQLSeolFSh3OyS_XpX1lRIJP-8CH8WG0X0hL98SM9d85LqC22Bow/viewform?usp=sf_link"]
function scrapeForms(){
formIds.forEach( formId => {
// The code below logs the HTML code of the Google home page.
var response = UrlFetchApp.fetch(formId);
results = response.getElementsByClassName("freebirdFormviewerViewItemsItemItemTitleContainer");
Logger.log(results.getContentText())
});
}
但得到的回报是:
TypeError: response.getElementsByClassName is not a function (ligne 13, fichier "Code")
根据 这个Javascript "require "是什么? require
并不是标准JavaScript的一部分,也不被Google Apps Script支持。
另一方面,由于Google Apps Script Chrome V8引擎不支持异步功能,所以这个错误信息不容易解决。相关内容 谷歌应用脚本是同步的吗?
如果你将使用Google Apps Script,并且你是表单所有者或表单编辑器,而不是试图网络抓取一个Google表单,使用Google Apps Script的表单服务。为此,你将需要表单 ../edit
URL,而不是 ../viewform
URLs。在官方文档中,有一个 速成 对你有帮助的 https:/developers.google.comapps-scriptquickstartforms。.
你可以用 openByUrl 来 "打开 "一个表单。它不会在你的网页浏览器中实际打开,而是在服务器端打开。那么你可以使用 getItems 来获取所有的问题、部分、图片、视频等。
如果您不是表单所有者或表单编辑器,那么您应该使用 UrlFetchApp
服务,并根据问题的位置以某种方式解析每个表单的网页源代码。相关问题。Google Sheets: 如何导入以下数据?
另外,如果表单有几个部分,你应该做一个post请求来模拟点击下一个按钮,以获得第二部分和以下部分。还有更多的 "如果表单有......",但我将在这里停止,因为问题的主要部分已经回答了,我想。
你首先要得到所有的表单,所以把这些表单放在一个数组中。
const formIds = ["someId", "anotherId", "andSoOn"]
然后,让我们使用FormApp来获取表单和所有项目。项目可以是不同的类型,见文档。
// this array will store forms and their questions
let formAndQuestions = [];
formIds.forEach( formId => {
const form = FormApp.openById( formId );
// lets get the name
const formName = form.getTitle();
// first we get all items
const allItemsInThisForm = form.getItems();
// then we get filter out anything that is not a questions
const allQuestionsInThisForm = allItemsInThisForm.filter( item => {
return isThisItemAQuestion( item )
});
// now we store them in our object
formAndQuestions.push( {
formId: formId,
formName: formName,
questions: allQuestionsInThisForm
}
});
// this function is used to only get the itemTypes you want
// see reference for more information
function isThisItemAQuestion( item ){
const itemType = item.getType();
const validQuestionItemTypes = [ FormApp.ItemType.TEXT, "add others here" ]
let isValid = false;
validQuestionItemsTypes.forEach( validItemType => {
if( itemType == validItemType ) {
isValid = true;
}
});
return isValid
}
然后,你可以初步登录出结果,看看它是什么样子的。
Logger.log( formAndQuestions )