簡體   English   中英

使用 Google 腳本 ReDash 到 Google 表格 - 如何在末尾添加日期?

[英]ReDash to Google Sheets using Google Script - How do I add the date at the end?

我正在嘗試使用以下 Google 腳本從 Redash 添加數據,然后在末尾添加當前日期。 var now = new Date(); 在最后一行輸入日期?

期望的結果如下。 AC 是我已經從 Redash 成功提取的數據,但我想添加當前日期。 如果有幫助,日期將始終為 F。

https://www.dropbox.com/s/lfu9jk06j0fduo8/Screenshot%202020-05-20%2018.44.26.png?dl=0

謝謝您的幫助!

//FUNCTION FOR GETTING DATA FROM REDASH QUERIES
function getapidata(response, sheetname) {
  var array1 = response.getContentText(); //Store data from redash into this variable
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
  var iLen = array1.length;
  var i = 0;
  var stringvalue1 = "";
  var s1 = "";
  var num1 = 1;
  var num2 = 1;
  var noheader = 1;
  for (i = 0; i < iLen; i++) {
    var j = i;
    do {
      s1 = array1[j];
      if (s1 == ",") {
        if (noheader == 1)
          noheader = 1;
        else
          sheet.getRange(sheet.getLastRow() + num1, num2).setValue(stringvalue1);
        num1 = 0;
        num2++;
        stringvalue1 = "";
      } else
        stringvalue1 = stringvalue1 + s1;
      j++;
    } while (s1 != "\n")
    if (noheader == 1)
      noheader = 1;
    else
      sheet.getRange(sheet.getLastRow() + num1, num2).setValue(stringvalue1);
    noheader = 2;
    num1 = 1;
    num2 = 1;
    stringvalue1 = "";
    i = j - 1;
  }
}

function getfromRedash() {
  //Current SHR (Max Preps)
  var redashapi = "API"; //Storing Redash API Value.
  var sheetname = "SHEETTITLE"; //Assign your sheetname(where you would want data to be imported) to this variable
  var response = UrlFetchApp.fetch(redashapi, {
    muteHttpExceptions: true
  }); //Storing Redash Cached Query result in this variable.
  Logger.log(response.getContentText()); //If any error, error to be logged
  getapidata(response, sheetname); //Call function for writing data in google sheets
  //The 5 lines of code above can be repeated for multiple sheets in a single google spreadsheet file. For. Eg. If you have 5 different sheets in a single google worksheet, you can just copy paste the above lines of code, and just change the variable “redashapi” so as to make calls to the appropriate redash queries.
}

//THIS FUNCTION IS TO MAKE A NEW MENU IN GOOGLE SHEETS
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('MENU')
    .addItem('Get Data from Redash', 'getfromRedash')
    .addToUi();
}

好吧,你的行號尋址有點混亂,因為 sheet.getLastRow() 每次你在最后一行之后添加值時都會改變,但你可以寫類似的東西

sheet.getRange(sheet.getLastRow() + num1, 6).setValue(new Date());

插入日期。 您可以參考其他答案,例如

如何格式化 JavaScript 日期

用於日期格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM