我有以下代碼:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // current spreadsheet
var old_sheet = ss.getActiveSheet(); // current (old) sheet
var new_sheet = old_sheet.copyTo(ss); // make a copy
SpreadsheetApp.flush()
new_sheet.setName(Utilities.formatDate(new Date(), 'GMT-7:00', 'w')); // set the name
new_sheet.getRange('A3').setValue(new Date(), 'GMT-7:00', 'w'); // set the cell A3
new_sheet.getRange('B14:H18').clear({contentsOnly: true}); // clears content in B14:H18
old_sheet.hideSheet(); // hide the old sheet
}
我遇到問題的部分如下:
new_sheet.getRange('A3').setValue(new Date(), 'GMT-7:00', 'w'); // set the cell A3
我的目標是將星期日日期回傳到單元格 A2 或將當前周數(從星期日開始)回傳到單元格 A3。
例如:10/31/2021 將放入 A2 或 45(周數)將放入 A3,從那里我可以使用谷歌表格中的常規公式在任一方向作業。
我試圖 setvalue(new date() --> using 'w' 但遇到了一個錯誤:
“例外:引數 ((class),String,String) 與 SpreadsheetApp.Range.setValue 的方法簽名不匹配。”
我在這里讀到的:如何解決“引數(數字 [])與 SpreadsheetApp.Range.setValues 的方法簽名不匹配”錯誤
意味著范圍是二維的,我嘗試使用 .push() 但老實說我不知道??該怎么做。
我知道我是否 setvalue(new date()) 會給我當前日期,但我不確定如何獲得從本周開始(星期日)開始的日期。
謝謝,如果我拼寫錯誤或沒有正確解釋,請告訴我。我的母語不是英語。
uj5u.com熱心網友回復:
單元格 A2 中的星期日日期
var d = new Date()
d.setDate (d.getDate() - d.getDay())
new_sheet.getRange('A2').setValue(d);
uj5u.com熱心網友回復:
這是獲取最后一個星期日的日期的方法:
function get_last_sunday() {
var today = new Date();
var day = today.getDay(); // 0 - Sunday, 1 - Monday, 2 - Tuesday, etc...
var sunday = new Date(today.getTime() - 24*60*60*1000*day);
return sunday;
}
console.log(get_last_sunday())
Probably you can use the formula this way:
new_sheet.getRange('A2').setValue(get_last_sunday());
To get week number you can use this function:
function get_week() {
return Utilities.formatDate(new Date(), 'GMT-7:00', 'w');
}
And respectively:
new_sheet.getRange('A3').setValue(get_week());
As for the rest I don't understand your goal. Especially the 'OR' condition.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/344383.html
