我希望有一种更干净的方法来编写此代码。我下面的内容完全按照预期的方式工作,但是,我必须在多个不同的部分中进行此操作,因此在我这样做之前,我正在寻找是否存在潜在地使用for循环的方法或可以缩短此代码和帮助的方法它运行得更快。
用户可以输入的图像数量从1至4不等。根据选择的数量,将为要放置的每个图像创建新行。因此,这会将电子表格的底部的行压低了许多行,因此需要考虑这一点,以确保无论用户选择多少图像,信息都仍会插入正确的位置。这是用Google表格中的Google Apps脚本编写的。如果下面的变量的值大于2,则用户想要在其中显示图像,小于3的用户则不需要图像,并且不会创建新行。
var graphic1_loc = 'A62';
var graphic2_loc = 'A63';
var graphic3_loc = 'A64';
var graphic4_loc = 'A65';
var graphic5_loc = 'A66';
var graphic1 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic1_loc).getValue();
var graphic2 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic2_loc).getValue();
var graphic3 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic3_loc).getValue();
var graphic4 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic4_loc).getValue();
var graphic5 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic5_loc).getValue();
var website_loc = 'D2';
var website_placement = 'B47';
var website_placement2 = 'B48';
var website_placement3 = 'B49';
var website_placement4 = 'B50';
var website_placement5 = 'B51';
if (graphic2.length < 3 && graphic3.length < 3 && graphic4.length < 3 && graphic5.length < 3) {
var website = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+website_loc).getValue()
SpreadsheetApp.getActiveSheet().getRange('Briefing!'+website_placement).setValue('For the latest forecast updates, visit ' + website+'.');
}
else if (((graphic2.length > 2) && (graphic3.length < 3) && (graphic4.length < 3) && (graphic5.length < 3)) ||
+((graphic2.length < 3) && (graphic3.length > 2) && (graphic4.length < 3) && (graphic5.length < 3)) ||
+((graphic2.length < 3) && (graphic3.length < 3) && (graphic4.length > 2) && (graphic5.length < 3)) ||
+((graphic2.length < 3) && (graphic3.length < 3) && (graphic4.length < 3) && (graphic5.length > 2))) {
var website = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+website_loc).getValue()
SpreadsheetApp.getActiveSheet().getRange('Briefing!'+website_placement2).setValue('For the latest forecast updates, visit ' + website+'.');
}
else if ((graphic2.length > 2 && graphic3.length > 2 && graphic4.length < 3 && graphic5.length < 3) ||
+(graphic2.length > 2 && graphic3.length < 3 && graphic4.length > 2 && graphic5.length < 3) ||
+(graphic2.length > 2 && graphic3.length < 3 && graphic4.length < 3 && graphic5.length > 2) ||
+(graphic2.length < 3 && graphic3.length > 2 && graphic4.length > 2 && graphic5.length < 3) ||
+(graphic2.length < 3 && graphic3.length > 2 && graphic4.length < 3 && graphic5.length > 2) ||
+(graphic2.length < 3 && graphic3.length < 3 && graphic4.length > 2 && graphic5.length > 2)) {
var website = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+website_loc).getValue()
SpreadsheetApp.getActiveSheet().getRange('Briefing!'+website_placement3).setValue('For the latest forecast updates, visit ' + website+'.');
}
else if (((graphic2.length > 2) && (graphic3.length > 2) && (graphic4.length > 2) && (graphic5.length < 3)) ||
+((graphic2.length > 2) && (graphic3.length > 2) && (graphic4.length < 3) && (graphic5.length > 2)) ||
+((graphic2.length > 2) && (graphic3.length < 3) && (graphic4.length > 2) && (graphic5.length > 2)) ||
+((graphic2.length < 3) && (graphic3.length > 2) && (graphic4.length > 2) && (graphic5.length > 2))) {
var website = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+website_loc).getValue()
SpreadsheetApp.getActiveSheet().getRange('Briefing!'+website_placement4).setValue('For the latest forecast updates, visit ' + website+'.');
}
else if ((graphic2.length > 2) && (graphic3.length > 2) && (graphic4.length > 2) && (graphic5.length > 2)) {
var website = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+website_loc).getValue()
SpreadsheetApp.getActiveSheet().getRange('Briefing!'+website_placement5).setValue('For the latest forecast updates, visit ' + website+'.');
}
答案 0 :(得分:2)
我相信您正在谈论代码分解。这实际上取决于<
语句中>
或if
的对齐方式,如果没有共同的模式,您可能会保持现状。
如果这样做,我的第一个尝试可能是将常见的if
语句重构为一个函数,例如(您的第一个else if
),
else if (((graphic2.length > 2) && (graphic3.length < 3) && (graphic4.length < 3) && (graphic5.length < 3)) ||...)
将其概括为一个函数,因为graphic2,3,4,5
一直被使用,
在比较之前创建一个数组
var graphic_arr = [graphic2.length, graphic3.length, graphic4.length, graphic5.length]
然后创建一个条件函数;
function condOne(arr1,arr2) {
return arr1[0] > arr2[0] && arr1[1] > arr2[1] && arr1[2] > arr2[2] && arr1[3] > arr2[3]
}
那么您的原始状况就会变成
else if (condOne(graphic_arr, [2,3,3,3]) ||...)
如果到处都有一些通用的模式,并且您可以创建和重复使用,例如condTwo()
等,则可以得到类似的
请注意:相比之下,每个条件函数具有不同的<
和>
if (condOne(graphic_arr, [ 4 digits])) || condTwo(graphic_arr, [ 4 digits])) || .....)
elseif (condThree(graphic_arr, [ new 4 digits]) || condTwo(graphic_arr, [another 4 digits]))
.....
运行速度取决于仅基于当前代码消耗的数据量,只要条件正确地执行了条件检查,条件检查就无关紧要。