Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am new to google script (and Javascript in general) and am trying to create a "Macro" which loops through a large table of data (Titled "Form response 1"), searches each row to see if the data in the 'To-arrive-date" column matches todays date. If the dates match, I want to copy-paste it into another sheet titled "Email". Below is the script I have written so far, but I keep getting an error stating that an unexpected "Var" is on line 15. However, I thought this var needed to be there for the variable "SrcRange"? Any help would be wonderful, thank you.

  var sSheet = SpreadsheetApp.getActiveSpreadsheet();
  var srcSheet = sSheet.getSheetByName("Form Responses 1");
  var tarSheet = sSheet.getSheetByName("Email");
  var lastRow = srcSheet.getLastRow();
  var Tsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1");
    var date = new Date();
    Tsheet.getRange(2, 11).setValue(date);
  
  for (var i = 2; i <= lastRow; i++) {
    var cell = srcSheet.getRange("C" + i);
    var val = cell.getValue();
    if (val == SrcSheet.getRange(2, 11).getValues()
      
     var srcRange = srcSheet.getRange("A" + i + ":F" + i);
      
      var tarRow = tarSheet.getLastRow();
      tarSheet.insertRowAfter(tarRow);
      var tarRange = tarSheet.getRange("A" + (tarRow+1) + ":F" + (tarRow+1));
      
      srcRange.copyTo(tarRange);
    }
  }

-Evan

question from:https://stackoverflow.com/questions/65943314/copy-paste-if-date-today-google-sheets-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
238 views
Welcome To Ask or Share your Answers For Others

1 Answer

Issue

Your IF statement inside the loop is incomplete. It lacks closing parenthesis as well as opening curly brace. I also noticed and commented out unnecessary codes and corrected a few specifically with regards to new Date() method.

Solution

Please see modified code below. Note that I only modified a few part of the code to make it work. I haven't done any optimization(if applicable). Also, this code is working fully on my end.

function myFunction() {
  var sSheet = SpreadsheetApp.getActiveSpreadsheet();
  var srcSheet = sSheet.getSheetByName("Form Responses 1");
  var tarSheet = sSheet.getSheetByName("Email");
  var lastRow = srcSheet.getLastRow();
  var tSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1");
  var date = new Date().toDateString();
  tSheet.getRange(2, 11).setValue(date);
  
  for (var i = 2; i <= lastRow; i++) {
    var cell = srcSheet.getRange("C" + i);
    var val = new Date(cell.getValue().toString()).toDateString();
    Logger.log(val+" "+date)
    if (val == date){
      Logger.log("test")
      var srcRange = srcSheet.getRange("A" + i + ":F" + i);
      
      var tarRow = tarSheet.getLastRow();
      // tarSheet.insertRowAfter(tarRow);
      var tarRange = tarSheet.getRange("A" + (tarRow+1) + ":F" + (tarRow+1));
      
      srcRange.copyTo(tarRange);
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...