我现在已经挣扎了一段时间,但却无法让它发挥作用。 基本上,我有一个Google Form和一个Google Spreadsheet。
function myFunction() {
createSurveyResponses();
}
function createSurveyResponses() {
// Open a form by ID and add a new text item.
var form = FormApp.openById('form_id');
var response = form.createResponse();
var sheet = SpreadsheetApp.openById('spreadsheet_id');
var getAct = sheet.getActiveSheet();
var data = sheet.getDataRange().getValues();
// Access the text item as a generic item.
var items = form.getItems();
var item = items[0];
var urls = [];
var resultUrls = [];
for (var j = 1; j < data.length; j++) {
var dop = data[j][0]
if (item.getType() == 'TEXT') {
var textItem = item.asTextItem();
var itemResponse = textItem.createResponse(data[j][0]);
var another = response.withItemResponse(itemResponse);
response.submit();
}
}
// get the responses from the spreadsheet
var fresponses = form.getResponses();
for (var i = 0; i < fresponses.length; i++) {
var resp = [fresponses[i]];
urls.push([shortenUrl(fresponses[i].getEditResponseUrl())]);
}
var getdata = getAct.getRange(2,5,fresponses.length)
getdata.setValues(urls);
}
function shortenUrl(longUrl) {
// google url shortener api key
var key = "AIzaSyBVG4Q5i1mNI0YAO0XVGZ3suZU8etTvK34";
var serviceUrl="https://www.googleapis.com/urlshortener/v1/url?key="+key;
var options={
muteHttpExceptions:true,
method:"post",
contentType: "application/json",
payload : JSON.stringify({'longUrl': longUrl })
};
var response = UrlFetchApp.fetch(serviceUrl, options);
if(response.getResponseCode() == 200) {
var content = JSON.parse(response.getContentText());
if ( (content != null) && (content["id"] != null) )
return content["id"];
}
return longUrl;
}
我想要完成的任务:
到目前为止,这是我的Google应用脚本:
Sorry, this response has already been submitted. (line 34, file "")
然而,当我运行代码时,在第一次迭代(第一行)之后,我收到错误response.submit();
,这是我提交回复{{1}}的时间。
我做错了什么?
我的最终目标是为每一行生成一个唯一的网址,以便我的收件人可以随时使用该网址更新他们的回复(getEditResponseUrl()
)。
答案 0 :(得分:5)
您需要在每个循环上创建一个新的响应。现在,您正在创建一个新的响应,并且在每次迭代时,代码都使用相同的表单响应。因此,您会收到已经提交的错误。
编辑:这是一些新代码。它有一个内部for
循环。外部for
循环遍历电子表格行的数量。内部for
循环遍历单个表单响应中的项目。
submit
方法不能在内循环中使用。每个表单项(问题)都必须使用createResponse()
输入答案,然后必须将项响应添加到表单响应中。单词 response 可用于表单响应整体或单个问题的响应。
然后将项目响应添加到表单响应中:
newResponse.withItemResponse(itemResponse);
方法withItemResponse
的名称让我很困惑。我希望我需要链接另一种方法,但事实并非如此。
以下代码正在运行:
function createSurveyResponses(ss_ID) {
if (ss_ID === undefined) {
ss_ID = '';
};
var ss = SpreadsheetApp.openById(ss_ID);
var sheet = ss.getSheetByName('Sheet1');
//Get data starting in row 2, column 2
var data = sheet.getRange(2, 2, sheet.getLastRow()-1, sheet.getLastColumn()-1).getValues();
var i = 0,
j = 0,
form,
items,
thisRow,
Name = "",
Email = "",
Revenue,
FormURL = "",
formID,
thisItem,
itemTypeIs,
response,
arraySS_Values = [],
editURL;
var arrayItemNames = ['Name','Email','Revenue'];
for (i=0;i<data.length;i+=1) {
thisRow = data[i];
Name = thisRow[0];
Email = thisRow[1];
Revenue = thisRow[2];
FormURL = thisRow[3];
arraySS_Values = [];
arraySS_Values.push(Name);//Fill an array with the cell values of one row from the spreadsheet
arraySS_Values.push(Email);
arraySS_Values.push(Revenue);
Logger.log('Name: ' + Name);
if (FormURL === "" || FormURL === undefined) { //If there is no form, create one
form = FormApp.create(Name);
formID = form.getId();
items = addItemsToForm(form, arrayItemNames);
} else {
form = FormApp.openByUrl(FormURL);
items = form.getItems(FormApp.ItemType.TEXT);
if (items.length === 0) { //If there are no form items, you must add them
items = addItemsToForm(form, arrayItemNames);
};
};
var newResponse = form.createResponse();
for (j=0;j<items.length;j+=1) {
thisItem = items[j];
itemTypeIs = thisItem.getType();
if (itemTypeIs===FormApp.ItemType.IMAGE || itemTypeIs===FormApp.ItemType.PAGE_BREAK || itemTypeIs===FormApp.ItemType.SECTION_HEADER) {
continue; //quit this loop, and loop again if the form item is an image, page break or section header
};
if (itemTypeIs === FormApp.ItemType.TEXT) {
var textItem = thisItem.asTextItem();
var itemResponse = textItem.createResponse(arraySS_Values[j]);
newResponse.withItemResponse(itemResponse);
Logger.log('itemResponse: ' + itemResponse.getResponse());
};
};
newResponse.submit();
var preFill_url = newResponse.toPrefilledUrl();
Logger.log('preFill_url: ' + preFill_url);
sheet.getRange(i+2, 5).setValue(preFill_url);
};
};
function addItemsToForm(form, arrayItemNames) {
var i=0;
for (i=0;i<arrayItemNames.length;i+=1) {
form.addTextItem().setTitle(arrayItemNames[i]);
};
return form.getItems();
};
代码试图处理表单是否已存在的情况。如果电子表格没有表单URL,则会创建一个新表单。我不知道您是否可以在此代码中使用缩短的网址,因为它可能需要使用网址打开表单。