Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用应用程序脚本将数据从google sheets发送到firestore云_Javascript_Google Sheets_Google Cloud Firestore - Fatal编程技术网

Javascript 使用应用程序脚本将数据从google sheets发送到firestore云

Javascript 使用应用程序脚本将数据从google sheets发送到firestore云,javascript,google-sheets,google-cloud-firestore,Javascript,Google Sheets,Google Cloud Firestore,我一直在尝试将数据从google电子表格发送到firestore数据库,但我不知道每个字段的标题,所以我想动态地从google sheets标题列(google sheets的第一行)获取字段名。下面是我使用的代码。如何从工作表本身动态添加字段名 function DBinsert() { const email = "xxxxxxxx.gserviceaccount.com"; const key = "-----BEGIN PRIVATE KEY--

我一直在尝试将数据从google电子表格发送到firestore数据库,但我不知道每个字段的标题,所以我想动态地从google sheets标题列(google sheets的第一行)获取字段名。下面是我使用的代码。如何从工作表本身动态添加字段名

function DBinsert() {
   const email = "xxxxxxxx.gserviceaccount.com";
   const key = "-----BEGIN PRIVATE KEY-----xxxxxx\n-----END PRIVATE KEY-----\n";
   const projectId = "db";
   var firestore = FirestoreApp.getFirestore (email, key, projectId);
   
// get document data from ther spreadsheet
   var sh1 = SpreadsheetApp.getActiveSpreadsheet();
   var sheet = sh1.getSheetByName("Sheet1"); 
   // get the last row and column in order to define range
   var sheetLR = sheet.getLastRow(); // get the last row
   var sheetLC = sheet.getLastColumn(); // get the last column
   var dataSR = 2; // the first row of data
   // define the data range
   var sourceRange = sheet.getRange(2,1,sheetLR-dataSR+1,sheetLC);
   // get the data
   var sourceData = sourceRange.getValues();
   // get the number of length of the object in order to establish a loop value
   var sourceLen = sourceData.length;
   // Loop through the rows
     for (var i=0;i<sourceLen;i++){
       if(sourceData[i][1] !== '') {
         var data = {};
           data.name = sourceData[i][0];
           data.age = sourceData[i][1];
           data.place = sourceData[i][2];
           data.address = sourceData[i][3];   
           firestore.createDocument("testsheet",data);
   }
  } 
}
函数DBinsert(){
const email=“xxxxxxxx.gserviceaccount.com”;
const key=“----开始私钥------xxxxxx\n----结束私钥------\n”;
const projectd=“db”;
var firestore=FirestoreApp.getFirestore(电子邮件、密钥、projectId);
//从电子表格中获取文档数据
var sh1=SpreadsheetApp.getActiveSpreadsheet();
var sheet=sh1.getSheetByName(“Sheet1”);
//获取最后一行和最后一列以定义范围
var sheetLR=sheet.getLastRow();//获取最后一行
var sheetLC=sheet.getLastColumn();//获取最后一列
var dataSR=2;//第一行数据
//定义数据范围
var sourceRange=sheet.getRange(2,1,sheetLR-dataSR+1,sheetLC);
//获取数据
var sourceData=sourceRange.getValues();
//获取对象的长度数,以便建立循环值
var sourceLen=sourceData.length;
//环行

对于(var i=0;i您的意思是想根据标题值动态设置JSON数据的字段名吗?确切地说是@MoniqueG。我想改为设置data.name,而不是设置标题值。我正在尝试开发一个自动脚本,该脚本每天接收每个电子表格并放入集合。因此每个电子表格可能都有不同的头。然后,您可以尝试使用以下方法:1.读取头并存储到数组中。例如:头[]2.设置数据时,使用以下语法:data[headers[0]]=sourceData[i][0];通过此操作,您已经可以动态设置标头了。@MoniqueG。非常感谢。它成功了。不客气!很高兴知道它成功了,并且对您有所帮助,@SarijaJanardh!您的意思是要根据标头值动态设置JSON数据的字段名吗?确切地说是@MoniqueG。我要设置标头,而不是设置data.name值。我正在尝试开发一个自动脚本,该脚本接受每个电子表格并每天放入集合。因此每个电子表格可能有不同的标题。然后,您可以尝试使用以下方法:1.读取标题并存储到数组中。例如:headers[]2.设置数据时,使用以下语法:data[headers[0]]=sourceData[i][0];通过它,您已经可以动态设置标题了。@MoniqueG。非常感谢您。它很有效。不客气!很高兴知道它有效并且对您有所帮助,@SarijaJanardh!