检查日期并插入带有列和行的工作表

时间:2019-06-26 10:03:12

标签: google-apps-script google-sheets

我正在尝试使工作报告自动化。数据从google表单输入到Google表格中,目前我已经完成了部分流程,请参见下面的代码。

var sheets = SpreadsheetApp.getActiveSpreadsheet();          //Gets the active spreadsheet.

var inputSheet = sheets.getSheetByName('Form');  //Gets sheet Form responses 1.

var lastRow = inputSheet.getLastRow();                       //Gets last row and stores it to variable lastRow.

var statSheet = sheets.getSheetByName('Nums');            //Gets numbers sheet.

var type = inputSheet.getRange('C' + lastRow).getValue();    //Gets column C which contains type of review i.e. compliment, comment or complaint.

var service = inputSheet.getRange('O' + lastRow).getValue(); //Gets column O which contains the service the review is assigned to.

var date = new Date(inputSheet.getRange('L' + lastRow).getValue());    //Gets column L which contains the date the review was submitted.
var month = date.getMonth();

var array = [["Befriending", "Comment", "B2"],["Befriending", "Compliment", "C2"],["Befriending", "Complaint", "D2"], ["Finance", "Comment", "B3"],["Finance", "Compliment", "C3"],["Finance", "Complaint", "D3"],

           ["Fund Raising", "Comment", "B4"],["Fund Raising", "Compliment", "C4"],["Fund Raising", "Complaint", "D4"],

           ["Home Help", "Comment", "B5"],["Home Help", "Compliment", "C5"],["Home Help", "Complaint", "D5"],

           ["I & A", "Comment", "B6"],["I & A", "Compliment", "C6"],["I & A", "Complaint", "D6"],

           ["Management Services", "Comment", "B7"],["Management Services", "Compliment", "C7"],["Management Services", "Complaint", "D7"],

           ["Toe Nails", "Comment", "B8"],["Toe Nails", "Compliment", "C8"],["Toe Nails", "Complaint", "D8"],

           ["Trading", "Comment", "B2"],["Trading  ", "Compliment", "C2"],["Trading  ", "Complaint", "D2"]  // Mutli-dimensional array containing the service, type of review and data range.
];           

for(m = 0; m < 12; m++) // For loop that iterates through m as an index less than 12 times.
{
  if(m === month){

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

    collectingData(statSheet, service, type, array[i][0], array[i][1], array[i][2]); // Calling function to pass through arguments if conditions are met.
  }
}
}


function collectingData(statSheet,service, type, numbersService, numbersType, numbersColumn)
{

if(service === numbersService && type === numbersType)   // If service and type conditions are met, value is added to currentTotal

  {
  var currentTotal = statSheet.getRange(numbersColumn).getValue();
currentTotal++ 
statSheet.getRange(numbersColumn).setValue(currentTotal);
}
}
}

上面的代码工作正常,但是,我需要做的是检查一个月是否存在,以及是否没有为要输入的数据创建新表。我对编码比较陌生,不知道是否可行?我相信是这样,但是我如何用代码写出来却让我感到困惑!

1 个答案:

答案 0 :(得分:0)

  • 转到代码编辑器,单击时钟按钮 在“保存”和“运行”之间。
  • 这将带您到右下角的触发站点 将会有一个“添加新触发器”按钮。
  • 单击它,然后可以选择最适合您的触发器。
  • 对于所需的对象,在“事件源”中选择“时间驱动”
  • 然后选择“每月”在每月的哪一天和一天中的什么时间。

以下图片为示例:

trigger button


trigger settings


此外,here是一些有关触发器的文档,以防您还有其他问题。