【问题标题】:Google Sheet Report with CSV Repository on Google Drive > Update report when new CSV is added to repositoryGoogle Drive 上带有 CSV 存储库的 Google Sheet Report > 将新 CSV 添加到存储库时更新报告
【发布时间】:2019-08-24 04:09:58
【问题描述】:

我正在使用 Google API 构建一个脚本,它将新的 CSV 文件附加到 Google 表格报告中。


GDrive 布局:

  • Google 表格 = 累计人工报告 > Tab1 = 数据透视表,Tab2 = 数据列表
  • 文件夹 = CSV 存储库 > 7 天劳动报告

目前,数据列表每周都会使用新 CSV 文件中的数据手动更新。


流程:

  1. Web 应用程序将 SQL 报告以 CSV 格式分发到 Gmail 帐户。
  2. 运行一个时间触发的脚本,将文件保存到 Google Drive 上的 CSV 存储库中。
  3. *运行时间触发的脚本,将新的 CSV 数据附加到数据列表中
  4. 新的 CSV 文件已重命名,以在末尾包含日期。

*我在流程中的第 3 步需要帮助。


我尝试了不同的方法,但最接近的方法是添加一个新的数据选项卡,而不是附加数据。

新标签的结果是:

var master = ss.insertnewdata(); 

这是我目前所拥有的:

  function importData() {

    // Folder ID of the repository containing all CSV files

    var fSource = DriveApp.getFolderById("Folder ID");
   
    // CSV file name of the last recieved report file
    var weeklyreport = fSource.getFilesByName('Project Time Reporting.csv');
    
    // Google Sheet ID containing labor report and data list
    var ss = Sheet ID');

    // Data list tab on Google Sheet
    var dataListSheet = ss.getSheetByName('Data List');
             
      // Last row on the data list tab.
    var lastRow = dataListSheet.getLastRow(); 
     // var ReportData =   
    

    
    if ( weeklyreport.hasNext() ) { // procced if CSV file name exists in the repository
      var file = weeklyreport.next();
      var csv = file.getBlob().getDataAsString();
      var csvData = CSVToArray(csv); // see below for CSVToArray function
               // loop through csv data array and append rows into Data List
      for ( var i=0, lenCsv=csvData.length; i<lenCsv; i+i ) {
        //After last row on data list, insert new row with all data from CSV.
    dataListSheet.getRange(lastRow + 1, 1, csvData.length, csvData[0].length).setValues(csvData);
        
      }
      
      // rename the report.csv file so it is not processed on next scheduled run
      file.setName("Weekly Labor Report Export-"+(new Date().toString())+".csv");
}


    function CSVToArray( strData, strDelimiter ) {
    // Check to see if the delimiter is defined. If not,
    // then default to COMMA.
    strDelimiter = (strDelimiter || ",");

    // Create a regular expression to parse the CSV values.
    var objPattern = new RegExp(
      (
        // Delimiters.
        "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

        // Quoted fields.
        "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

        // Standard fields.
        "([^\"\\" + strDelimiter + "\\r\\n]*))"
      ),
      "gi"
    );

    // Create an array to hold our data. Give the array
    // a default empty first row.
    var arrData = [[]];

    // Create an array to hold our individual pattern
    // matching groups.
    var arrMatches = null;

    // Keep looping over the regular expression matches
    // until we can no longer find a match.
    while (arrMatches = objPattern.exec( strData )){

      // Get the delimiter that was found.
      var strMatchedDelimiter = arrMatches[ 1 ];

      // Check to see if the given delimiter has a length
      // (is not the start of string) and if it matches
      // field delimiter. If id does not, then we know
      // that this delimiter is a row delimiter.
      if (
        strMatchedDelimiter.length &&
        (strMatchedDelimiter != strDelimiter)
      ){

        // Since we have reached a new row of data,
        // add an empty row to our data array.
        arrData.push( [] );

      }

      // Now that we have our delimiter out of the way,
      // let's check to see which kind of value we
      // captured (quoted or unquoted).
      if (arrMatches[ 2 ]){

        // We found a quoted value. When we capture
        // this value, unescape any double quotes.
        var strMatchedValue = arrMatches[ 2 ].replace(
          new RegExp( "\"\"", "g" ),
          "\""
        );

      } else {

        // We found a non-quoted value.
        var strMatchedValue = arrMatches[ 3 ];

      }

      // Now that we have our value string, let's add
      // it to the data array.
      arrData[ arrData.length - 1 ].push( strMatchedValue );
    }

    // Return the parsed data.
    return( arrData );
  };

【问题讨论】:

    标签: google-apps-script google-api google-api-client


    【解决方案1】:

    importData() 函数中,定义一个变量,该变量将是Data List 选项卡:

    var dataListSheet = ss.getSheetByName('Data List');
    

    找到Data List标签的最后一行:

    var lastRow = dataListSheet.getLastRow();
    

    将导入的数据添加到lastRow下面的行:

    dataListSheet.getRange(lastRow + 1, 1, csvData.length, csvData[0].length).setValues(csvData)
    

    【讨论】:

    • 谢谢,它现在正在将信息添加到数据列表选项卡,但不幸的是现在它返回错误:执行失败:数据中的列数与列数不匹配在范围内。数据有 1 但范围有 12 (第 28 行) 查看更新的脚本。
    • 错误导致 .csv 文件无法重命名。
    • 您需要记录 CSV 数组并检查它,以了解脚本为什么会看到列数不匹配。重新将导入的数据添加到工作表中,您已将其放置在循环中。但是该行一次性发布了整个 CSV 数组。在循环内部,它只是重复“粘贴”工作。
    猜你喜欢
    • 2015-12-05
    • 2012-09-23
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多