I believe your goal as follows.
You have a sample Spreadsheet like below.
You want to retrieve the values and put them to Firestore as follows.
You want to achieve this using Google Apps Script.
Modification points:
- In this case, I would like to propose the following flow.
- Retrieve values from Spreadsheet.
- Create an object for sending to Firestore.
- Send the object to Firestore.
When above flow is reflected to the sample script, it becomes as follows.
Modified script:
Before you use this script, please set the variables of spreadsheetId
, sheetName
and email, key, projectId
of FirestoreApp.getFirestore(email, key, projectId)
, and also, collectionName
.
const spreadsheetId = "###"; // Please set the Spreadsheet ID.
const sheetName = "Sheet1"; // Please set the sheet name.
// 1. Retrieve values from Spreadsheet.
const sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
const [header, ...data] = sheet.getDataRange().getValues();
// 2. Create an object for sending to Firestore.
const obj = data.reduce((ar, r) => {
const temp = {};
r.forEach((c, j) => {
let key = header[j];
if (key.includes("Tags")) {
key = "tags";
temp[key] = temp[key] ? temp[key].concat(c) : [c];
} else {
temp[key] = c;
}
});
ar.push(temp);
return ar;
}, []);
// 3. Send the object to Firestore.
const firestore = FirestoreApp.getFirestore(email, key, projectId);
firestore.createDocument(collectionName, obj);
Result:
When this script is run, the following situation can be obtained.
From:
To
Note:
- In this sample script, the sample Spreadsheet I show you above from your updated question is used. So, when your actual situation is different from it, the script might not be able to be used. Please be careful this.
- In your sample Spreadsheet, I understood that the headers of
tags
are Tags 0
, Tags 1
and Tags 2
. This script is for the headers. Please be careful this. If your actual situation is not correct, please modify the script.
- In this sample script, it supposes that you have already been able to get and put values to Firestore. Please be careful this.
- Please use this script with enabling V8 runtime.
References:
Added:
About the following your request in your replying,
- I want each row in the spreadsheet to be its own unique document within a collection. 2) Tags are the only sets of data that need to be in array on the document. The rest of them need to be just strings
How about modifying above script as follows?
From:
// 3. Send the object to Firestore.
const firestore = FirestoreApp.getFirestore(email, key, projectId);
firestore.createDocument(collectionName, obj);
To:
// 3. Send the object to Firestore.
const firestore = FirestoreApp.getFirestore(email, key, projectId);
obj.forEach(r => firestore.createDocument(collectionName, r));
- In your current script,
collectionName
is used? Or scores
is used? Unfortunately, from your replying, I couldn't understand about this. So I just modified my proposed script. So please modify the variable name for your actual situation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…