Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

javascript - Firestore > Google sheets app script, remove StringValue, etc

I got an Appscript created for Google sheets to pull my Firestore database into Google Sheets on command. However, its pulling data with "StringValue" included in the data as well. How do I remove this? (see screenshots) enter image description here

function onOpen() {
 SpreadsheetApp.getUi().createMenu('?? Firestore').addItem('Import DB', 'importFromFirestore').addToUi();
}


// took my key out for obvious reasons. Replaced with XXXXXXXX - I know its right b/c it pulls the right data
function getFirestore() {
 return FirestoreApp.getFirestore('XXXXXX', 'XXXXXXX', 'XXXXXXXX'); 
  }

function importFromFirestore() {
  // 1. Get a Firestore insance
  const firestore = getFirestore(); 
  
  // 2. Get a collection from Firestore
  const allDocuments = firestore.getDocuments('scores').map(function(document) {
    return document.fields;
  });
  
  // 3. Get the first document from the collection
  const first = allDocuments[0];
  const columns = Object.keys(first);
  
  const sheet = SpreadsheetApp.getActiveSheet();
  sheet.appendRow(columns);
  
  // 4. Turn each document into an array to be appended to the sheet
  allDocuments.forEach(function(document) {
    const row = columns.map(function(column) {
      return document[column];
    });
    sheet.appendRow(row);
  });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your code change:

Before

// 2. Get a collection from Firestore
const allDocuments = firestore.getDocuments('scores').map(function(document) {
return document.fields;
});

After

// 2. Get a collection from Firestore
const allDocuments = firestore.getDocuments('scores').map(function(document) {
return document.obj;
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...