Electro69

Untitled

Dec 19th, 2023
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. I want the below Google Apps script to run whenever a google search is made and it should update my google sheets page
  2.  
  3. function addSearch() {
  4. // Access the active sheet
  5. var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  6.  
  7. // Get today's date
  8. var today = new Date();
  9. var formattedDate = Utilities.formatDate(today, "GMT+5:30", "yyyy-MM-dd");
  10.  
  11. // Get the last updated date and search count from the sheet
  12. var lastUpdatedDate = sheet.getRange('A2').getValue();
  13. var searchCount = sheet.getRange('B2').getValue();
  14.  
  15. // Calculate the time difference in hours
  16. var timeDifference = (today - lastUpdatedDate) / (1000 * 60 * 60);
  17.  
  18. // If it has been more than 24 hours since the last update, add a new row with the current date
  19. if (timeDifference >= 24) {
  20. sheet.insertRowAfter(1);
  21. sheet.getRange('A2').setValue(formattedDate);
  22. sheet.getRange('B2').setValue(1); // Reset search count for the new day
  23. } else {
  24. // If it's the same day, increment the search count
  25. sheet.getRange('B2').setValue(searchCount + 1);
  26. }
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment