I’m working with a spreadsheet that contains lengthy text entries in column H, which come from form submissions. My goal is to create a document where each text response appears on its own page along with its corresponding ID number from column I.
Current Setup
| Date | User | Contact | Field1 | Field2 | Consent1 | Consent2 | Feedback | ID |
|---|---|---|---|---|---|---|---|---|
| 3/14/2025 09:15:22 | Alice | [email protected] | Option1 | Option2 | Agreed | Agreed | Sample feedback text for document | 1 |
| 3/14/2025 11:30:45 | Bob | [email protected] | Option1 | Option2 | Agreed | Agreed | Additional feedback for second page | 2 |
Expected Output
Page 1:
1
Sample feedback text for document
Page 2:
2
Additional feedback for second page
My Current Code
function transferData() {
const workbook = SpreadsheetApp.getActiveSpreadsheet();
const worksheet = workbook.getActiveSheet();
const totalRows = worksheet.getLastRow();
const newDoc = DocumentApp.create("Feedback Responses Collection");
const docBody = newDoc.getBody();
var totalCols = worksheet.getLastColumn();
var allData = worksheet.getSheetValues(1, 1, totalRows, totalCols);
for(let i = 0; i < totalRows; i++){
var itemNumber = i + 1;
var feedbackText = allData[itemNumber][7];
docBody.appendParagraph(itemNumber);
docBody.appendParagraph(feedbackText);
docBody.appendPageBreak();
}
}
Error Message
TypeError: Cannot read properties of undefined (reading '7')
transferData @ Code.gs:21
I’ve tried different approaches like using index 8, hardcoding values, adjusting the loop to start from 1, but nothing works. Any ideas what might be causing this issue?