Я пишу код в редакторе Google Apps Scipt для экспорта данных из Google Sheets в Google Firebase. Прямо сейчас мне нужно жестко запрограммировать точное количество имеющихся у меня строк данных. Как я могу отредактировать свой код, чтобы продолжить go через лист Google, пока он не наткнется на пустую строку?
//Create a menu item for 'Export to Firestore'
function onOpen() {
SpreadsheetApp.getUi().createMenu('? Firebase').addItem('Export to Firestore', 'main').addToUi();
}
function main() {
//Get the active spreadsheet and its name for the collection name
var sheet = SpreadsheetApp.getActiveSheet();
var sheetName = sheet.getName();
//Get the first row as object properties
// ['Author', 'ISBN', 'Category', 'Title']
var properties = getProperties(sheet);
//Get the next 4 rows as records
//[ ['Hansley Ford', '34787426409', 'Drama', 'Grim Peeks'], ..., ...]
var records = getRecords(sheet);
//Export to Firestore
var firestore = FirestoreApp.getFirestore('','','');
exportToFirestore(firestore, sheetName, properties, records);
}
function exportToFirestore(firestore, collectionName, properties, records) {
records.map(function(record) {
//record: ['Hansley Ford', '34787426409', 'Drama', 'Grim Peeks']
//props: ['Author', 'ISBN', 'Category', 'Title']
var data = {};
properties.forEach(function(prop,i) { data[prop] = record[i]; });
return data;
}).forEach(function(data) {
firestore.createDocument(collectionName, data);
});
}
function getProperties(sheet) {
return sheet.getRange(1, 1, 1, 4).getValues()[0]; //['Author', 'ISBN', 'Category', 'Title']
}