我正在尝试为我的电子表格创建Google Apps脚本。它应该运行一个函数,并根据今天的日期将结果返回到电子表格中的特定工作表。只是将行附加到活动工作表而不是特定工作表时,它可以正常工作。此外,代码在脚本编辑器中保存得很好,所以我认为没有重大错误。也许我在Javascript中列出错误或者我不理解的代码中的基本错误。我倾向于我的IF ELSE或我如何设置活动电子表格然后表,但不确定。任何帮助,将不胜感激。以下是代码基础知识:
function CheckDateInsertData() {
function infoDate(){ this gets today's date}
if (infoDate() >= 2016/01/01 && infoDate() <= 2016/01/31) {
function GetData1{
//this retrieves necessary data, working when not using IF ELSE statements and only single active sheet, without calling sheet by name.
var Data =.......... ;
appendData(Data);
function appendData(Data){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.setActiveSheet(sheet.getSheetByName("January"));
sheet.appendRow(['Date', 'Price', 'Location']);
}
}
} else if (infoDate() >= 2016/02/01 && infoDate() <= 2016/02/29){
function GetData2{
var Data = ........;
appendData(Data);
function appendData(Data){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.setActiveSheet(sheet.getSheetByName("February"));
sheet.appendRow(['Date', 'Price', 'Location']);
}
}
}
}
答案 0 :(得分:0)
这就是我如何布置代码:
function CheckDateInsertData() {
var todaysDate = infoDate();
var startDate = new Date('2016/01/01');
var endDate = new Date('2016/01/01');
var startDateTwo = new Date('2016/02/01');
var endDateTwo = new Date('2016/02/01');
if (todaysDate >= startDate && todaysDate <= endDate) {
GetData('1'); //Pass '1' as the argument
} else if (todaysDate >= startDateTwo && todaysDate <= endDateTwo) {
GetData('2');
};
};
function appendData(Data, oneOrTwo){
var sheetToGet = oneOrTwo === '1'?"January":"February";//If, then, else
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.setActiveSheet(sheet.getSheetByName(sheetToGet));
sheet.appendRow(['Date', 'Price', 'Location']);
};
function GetData(oneOrTwo) {//this retrieves necessary data
var Data;
if (oneOrTwo === '1') {
Data = ['one'];
appendData(Data, oneOrTwo);
} else {
Data = ['two'];
appendData(Data, oneOrTwo);
};
};
function infoDate() {
return new Date();// this gets today's date
};
我不知道这是否会解决您的问题,但希望它会给您一些想法。