用于复制和重复行的Google脚本

时间:2017-07-14 08:01:44

标签: google-apps-script google-sheets

大家好我根本不喜欢脚本,只需要一些帮助,我想要做的是根据单元格F和G中的信息复制和重复单元格a,b,c,d,e。 / p>

在单元格d和e中是日期,它们需要在单元格F中按值增加并且根据G中的值继续重复,我已经制作了样本表,在工作表输入上是数据,在工作表输出上显示我需要的结果。但是所有信息都需要保留在输入表上,而不是另一张表。希望你在看样品表时能理解我的意思。在单元格中的表格中,它表示几周,这将在几天甚至几周内变得更好。

Title   Description     Location        Start Time      End Time     repeate every  how many times to repeat                                                                            
test    tester          here            9/7/17 20:00    9/7/17 20:10    2 weeks     4                                                                           
fake    data            home            15/7/17 15:00   15/7/17 16:00   4 weeks     3

original Google sheet

我想要获得的总体目标是能够在使用表单提交时自动将重复出现的事件添加到Google日历中,但是如果我可以在表单中重复我需要的数据,那么我可以将其同步到自动生成日历然后我可以根据天数设置重复发生的事件,因为我相信日历的重复活动只允许你每周每周做一次..

或者有人能指出我可以找到一些信息来帮助我吗?

2 个答案:

答案 0 :(得分:0)

我将重复更改为几天而不是几周。

function repeatingEvents() 
{
    var ss=SpreadsheetApp.getActiveSpreadsheet();
    var sht=ss.getSheetByName('Repeat');
    var rng=sht.getDataRange();//Get all of the data on the sheet
    var rngA=rng.getValues();
    var day=86400000;//milliseconds in a day
    var week=604800000;//millisecond in a week
    for(var i=0;i<rngA.length;i++)
    {
      if(rngA[i][6]>0)//If the number of repeats is greater than zero
      {
        var cnt=Number(rngA[i][6]);//cnt is number of repeats
        for(var n=0;n<cnt;n++)
        {
          var row=[];
          row.push(rngA[i][0]);//column A
          row.push(rngA[i][1]);//column B
          row.push(rngA[i][2]);//column C
          row.push(new Date(rngA[i][3].getTime() + (rngA[i][5] * (n + 1) * day)));//old date time plus repeat duration * repeat count * milliseconds in a day in column D
          row.push(new Date(rngA[i][4].getTime() + (rngA[i][5] * (n + 1) * day)));//column E
          row.push(rngA[i][5]);//Column F
          row.push('');//column G get zeroed out so script won't come back and do it again without human interaction
          sht.appendRow(row);//add new event
         }
         sht.getRange(i+1,7).setValue(0);//zero out initial number of repeats because I use this to determine when repeats are required
      }
    }
    sht.getRange(2,1,sht.getLastRow(),sht.getLastColumn()).sort({column:4,ascending:true});//Sort Spreadsheet by starting date
}

这是我的电子表格的样子。

enter image description here

好的,我更改了代码以适应您最近添加ID的更改。它已经在你的电子表格中,我在这里重复一遍。供参考。

function repeatingEvents() 
{
    var ss=SpreadsheetApp.getActiveSpreadsheet();
    var sht=ss.getSheetByName('input');
    var rng=sht.getDataRange();//Get all of the data on the sheet
    var rngA=rng.getValues();
    var day=86400000;//milliseconds in a day
    var week=604800000;//millisecond in a week
    for(var i=0;i<rngA.length;i++)
    {
      if(rngA[i][7]>0)//If the number of repeats is greater than zero
      {
        var cnt=Number(rngA[i][7]);//cnt is number of repeats
        for(var n=0;n<cnt;n++)
        {
          var row=[];
          row.push(rngA[i][0]);//A
          row.push(rngA[i][1]);//B
          row.push(rngA[i][2]);//C
          row.push(new Date(rngA[i][3].getTime() + (rngA[i][6] * (n + 1) * day)));//old date time plus repeat duration * repeat count * milliseconds in a day D
          row.push(new Date(rngA[i][4].getTime() + (rngA[i][6] * (n + 1) * day)));//E
          row.push(rngA[i][5]);//F
          row.push(rngA[i][6]);//G
          row.push('');//H
          sht.appendRow(row);//add new event
         }
         sht.getRange(i+1,8).setValue(0);//zero out initial number of repeats because I use this to determine when repeats are required
      }
    }
    sht.getRange(2,1,sht.getLastRow(),sht.getLastColumn()).sort({column:4,ascending:true});//Sort Spreadsheet by starting date
}

答案 1 :(得分:0)

我已将列F和G更改为G和H,我尝试编辑脚本以使用此功能,所有这些似乎都有效,除非它添加重复日期仍然将它们添加到F中而不是G似乎找出原因?

&#13;
&#13;
function repeatingEvents() 
{
    var ss=SpreadsheetApp.getActiveSpreadsheet();
    var sht=ss.getSheetByName('Calendar');
    var rng=sht.getDataRange();//Get all of the data on the sheet
    var rngA=rng.getValues();
    var day=86400000;//milliseconds in a day
    var week=604800000;//millisecond in a week
    for(var i=0;i<rngA.length;i++)
    {
      if(rngA[i][7]>0)//If the number of repeats is greater than zero
      {
        var cnt=Number(rngA[i][7]);//cnt is number of repeats
        for(var n=0;n<cnt;n++)
        {
          var row=[];
          row.push(rngA[i][0]);//A
          row.push(rngA[i][1]);//B
          row.push(rngA[i][2]);//C
          row.push(new Date(rngA[i][3].getTime() + (rngA[i][6] * (n + 1) * day)));//old date time plus repeat duration * repeat count * milliseconds in a day D
          row.push(new Date(rngA[i][4].getTime() + (rngA[i][6] * (n + 1) * day)));//E
          row.push(rngA[i][5]);//E This needs to be here other wise 6 and 7 become 5 & 6
          row.push(rngA[i][6]);//F
          row.push(rngA[i][7]);//G
          sht.appendRow(row);//add new event
         }
         sht.getRange(i+1,8).setValue(0);//zero out initial number of repeats because I use this to determine when repeats are required
      }
    }
    sht.getRange(2,1,sht.getLastRow(),sht.getLastColumn()).sort({column:4,ascending:true});//Sort Spreadsheet by starting date
}
&#13;
&#13;
&#13;