User enters information on Form A. On form A submission, an on submit trigger runs a sheet side appscript function to look up information (no issues there) and then runs a function to populate a short text type response on Form B. Form A and Form B are hosted on the same Google sites page. Form B does not populate
I have read and experimented with everything I can find on this site. I can get the code to run but I can’t get data to actually populate in form B.
I even tried to build an HMTL form using webapp but ran into same problem I can’t programmatically change response text on the Google sites page.
I’ve tried to reference form B through:
- getting the URL from the sheet linked to form B
- using the form Id from either the edit or view source URL
- Getting form B prefill link and replacing items
I tried to adapt the following answers located at: Answer by Mogsdad
- Apps Script - Programmatically submit answers from Google Sheet to Google Form - ERROR - "Sorry, this response has already been submitted."
- how to create a google form response with app script
- https://spreadsheet.dev/submit-responses-to-google-form-apps-script
//Function run by onsubmit trigger
//form B prefill code
function prefill(){
//get url using form B sheet
var sheet_ID ='sheet B ID';
const ss = SpreadsheetApp.openById(sheet_ID);
const sheet = ss.getSheetByName('Form Responses 1');
var formUrl = ss.getFormUrl();
console.log(formUrl)
// open form B- get form items - create response array
var form = FormApp.openByUrl(formUrl);
var items = form.getItems();
var formResponse = form.createResponse();
// Prefill a short text response
var formItem = items[0].asTextItem();
var response = formItem.createResponse("my data");
formResponse.withItemResponse(response);
// submit response ( also tried urlfetch post method)
formResponse.submit();
// nothing fills on Form B
};