javascript 火力基地|Google表单|应用程序脚本|如何将Firebase中的数据以列而非行的形式保存到Google Sheet?

1l5u6lss  于 2022-12-28  发布在  Java
关注(0)|答案(2)|浏览(86)

我在这里只是一个初学者,所以我希望你能对我有耐心:)
我设法将我的Firebase数据库同步到我的Google工作表,并在我的工作表中显示数据,但问题是,我想在列中显示数据,而不是行。
以下是实际结果:
enter image description here
这就是我想要达到的目标
enter image description here
下面是应用程序脚本中的代码:

function getAllData() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var sheet = ss.getActiveSheet();

  var firebaseUrl = "firebase url";

  var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);

  var dataSet = [base.getData()];

  var rows = [],
      data;

  for (i = 0; i < dataSet.length; i++) {

    data = dataSet[i];
    var in_time = Utilities.formatDate(new Date(), "GMT+8", "HH:mm:ss" );
    var in_date = Utilities.formatDate(new Date(), "GMT+8", "yyyy-MM-dd" );
    rows.push([data.PUMP, data.Tank1, data.Tank2, data.Tank3, data.Valve1, data.Valve2, data.Valve3, data.RandomMain, data.RandomNumber1, data.RandomNumber2, data.RandomNumber3, data.RandomNumber4 , in_date, in_time]);

  }

  dataRange = sheet.getRange(2, 1, rows.length, 14).setValues(rows);
  
}

希望你能帮助我这一点,并感谢您未来的React!!

alen0pnh

alen0pnh1#

当你的脚本被修改时,下面的修改怎么样?

修改的脚本:

function getAllData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  // var sheets = ss.getSheets(); // This is not used.
  var sheet = ss.getActiveSheet();
  var firebaseUrl = "firebase url";
  var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
  var dataSet = [base.getData()];

  // I modified the below script.
  var header = ["PUMP", "Tank 1", "Tank 2", "Tank 3", "Valve 1", "Valve 2", "Valve 3", "Random from Main", "Random from Tank 1", "Random from Tank 2", "Random from Tank 3", "Random from Tank 4", "Date", "Time"];
  var rows = [header], data;
  var in_time = Utilities.formatDate(new Date(), "GMT+8", "HH:mm:ss");
  var in_date = Utilities.formatDate(new Date(), "GMT+8", "yyyy-MM-dd");
  for (i = 0; i < dataSet.length; i++) {
    data = dataSet[i];
    rows.push([data.PUMP, data.Tank1, data.Tank2, data.Tank3, data.Valve1, data.Valve2, data.Valve3, data.RandomMain, data.RandomNumber1, data.RandomNumber2, data.RandomNumber3, data.RandomNumber4, in_date, in_time]);
  }
  rows = rows[0].map((_, c) => rows.map(r => r[c]));
  dataRange = sheet.getRange(2, 16, rows.length, rows[0].length).setValues(rows);
}
  • 运行此脚本时,将从列"P"(包括标题列)放入值。
  • 如果你不想包含标题列,请测试下面的脚本。在这种情况下,它假设标题列已经被设置为列"P"。请小心。
function getAllData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  // var sheets = ss.getSheets(); // This is not used.
  var sheet = ss.getActiveSheet();
  var firebaseUrl = "firebase url";
  var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
  var dataSet = [base.getData()];

  // I modified the below script.
  var rows = [], data;
  var in_time = Utilities.formatDate(new Date(), "GMT+8", "HH:mm:ss");
  var in_date = Utilities.formatDate(new Date(), "GMT+8", "yyyy-MM-dd");
  for (i = 0; i < dataSet.length; i++) {
    data = dataSet[i];
    rows.push([data.PUMP, data.Tank1, data.Tank2, data.Tank3, data.Valve1, data.Valve2, data.Valve3, data.RandomMain, data.RandomNumber1, data.RandomNumber2, data.RandomNumber3, data.RandomNumber4, in_date, in_time]);
  }
  rows = rows[0].map((_, c) => rows.map(r => r[c]));
  dataRange = sheet.getRange(2, 17, rows.length, rows[0].length).setValues(rows);
}

参考:

mwecs4sa

mwecs4sa2#

你的命令

sheet.getRange(2, 1, rows.length, 14).setValues(rows);

设置为填充行,即从单元格(2,1)开始,同时填充等于数组长度的行数和14列。您必须将其设置为:

sheet.getRange(1, 16, rows.length, 1).setValues(rows);

因此它将从单元(1,16)开始,并且仅填充一列,其行数与阵列的长度相同。
是否有理由将它赋给某个dataRange变量,因为您可以直接setValues?
文件。

相关问题