简体   繁体   中英

time stamp in multiple sheets in workbook

I have a daily worksheet with 6 sheets but only the first 4 do I need to enter the date just before they run the back up archive so in cell A1 todays date is added. All daily sheets have todays date and day ie 08.09.17 Friday as the title but they can be made many months in advance so when doing the back up (already working) I need todays date in cell A1 for all 4 sheets so they get moved to correct archive sheet for the date, which is what back up script is using to put in archive sheet date column so we can track customers daily usage. I am using this script but getting a blank output:-

    function timeStamp() {
  SpreadsheetApp.getActiveSheet()
     var sheetNames = ['1st sheet name', '2nd sheet name', '3rd sheet name', 
     '4th sheet name'];
     .setActiveCell(A1));
     .setValue(new Date());

Assuming that the sheets that you want this in are in the same spreadsheet then all you have to do is go to A1 and enter this =nameOfSpreadsheet() Yes, I know it's customary to capitalize all of the letters but I generally swim against the currents of following customs. You may capitalize all of them if you wish.

function nameOfSpreadsheet()
{
  return SpreadsheetApp.getActive().getName();
}

Also of course you will have to put the function into a project in your script editor. I keep my custom functions like this in a separate project from other functions.

I couldn't fit response to Paul's question in comments, so here we go.
Not sure if this is what the final solution is but...

function timeStamp() {
   var sheetNames = ['1st sheet name', '2nd sheet name', '3rd sheet name','4th sheet name'];
   for(i=0;i<sheetNames.length;i++)
      SpreadsheetApp.getActive().getSheetByName(sheetNames[i]).getRange("A1").setValue(new Date())
}

A bit of explanation..
SpreadsheetApp
Instance of the Application "Google SpreadSheets"

SpreadsheetApp.getActive()
This returns the active SpreadSheet(like Workbook/Full Document),could use .getActiveSpreadSheet() as well

SpreadsheetApp.getActive().getSheetByName(sheetNames[i])
This returns the Sheet with name sheetNames[i] (vis-a-vis Worksheet/Tab)

.getRange("A1").setValue(new Date())
Sets your date in "A1" for respective Sheet

Just a thought on what you had
The below would work

//You can drop the semicolon here if you want.
var sheetNames = ['1st sheet name', '2nd sheet name', '3rd sheet name', '4th sheet name']; 
//Moved it one line down as the next two lines run off of it by referencing it with a .(well almost)
SpreadsheetApp.getActiveSheet()
   //If line above is array assignment then no matter what you do you will get syntax error for the below two lines.       
   // You need to pass the address as string, unless A1 is a variable with string address.
  .getActiveCell("A1") 
   //Note that for this to work there should be no semicolons for the two lines of code above this.
  .setValue(new Date()) 
//Semicolon marks the end of statement and thus losing reference to an object and making any .function calls syntax errors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM