I am trying to create a file, in a specific folder for each row in a Google sheet. I would like the title of the files to be the content of the 1st cell of each row.
My script seems to be working except for the title. The files are named "untitled" and I can't figure out why. Here is my code
function iterateThroughRows() {
var sheet = SpreadsheetApp.getActive()
var data = sheet.getDataRange().getValues();
data.forEach(function (row) {
var values = SpreadsheetApp.getActiveSheet().getRange("A:A").getValues();
var folderId = "XXX"
var resource = {
title: values,
mimeType: MimeType.GOOGLE_SHEETS,
parents: [{ id: folderId }]
}
var fileJson = Drive.Files.insert(resource)
var fileId = fileJson.id
});
}
CodePudding user response:
You need to use row
to access the first element of array/or first cell, no need to use SpreadsheetApp.getActiveSheet().getRange("A:A").getValues();
as you already have the data.
Try following modification:-
function iterateThroughRows() {
var sheet = SpreadsheetApp.getActive()
var data = sheet.getDataRange().getValues();
data.forEach(function (row) {
var folderId = "XXX"
var resource = {
title: row[0], // First Cell/first element of array data
mimeType: MimeType.GOOGLE_SHEETS,
parents: [{ id: folderId }]
}
var fileJson = Drive.Files.insert(resource)
var fileId = fileJson.id
});
}