我最近在Excel中制作了一个针脚计数器,以便在编织时跟踪我的缝线,并决定尝试将VBA翻译成Google表格。经过两周的挣扎,我终于得到了几乎所有可以在Google工作的Excel工作的东西......除了一件事。
我有四个按钮,分配了四个独立但相似的代码。这4个功能中的每一个都会改变针迹计数器,唯一的区别是添加了多少针迹。
所有这些按钮都能正常工作,除非按下按钮时每个字段都是空白的。这就是问题所在:根据按下的按钮(所有字段都是空白),还会添加一行(这不是所需的结果)。
应该发生什么:当所有字段都是空白时,添加针迹会触发提示,让用户设置每行针数,然后根据使用的按钮添加针迹。 (如果"添加1个针迹"或者#34;根据用户编号添加针迹"确实会发生这种情况)
实际发生的事情:当所有字段都为空白时,如果"添加2针"或者"添加10针"如果按下,则提示用户设置每行针数,然后立即提示将其行设置为完成(一旦到达第一行就会触发),其行数设置为1,并且正确的针数已添加。
我不能为我的生活弄清楚为什么有一半的功能表现为一种方式而另一半则表现为另一种功能。
以下是我到目前为止编写的代码。希望有人能够看到有效的两个功能和不兼容的两个功能之间的区别,因为它一直暗指着我。
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0]; // sheets are counted starting from 0
var stitchCount = sheet.getRange("D2:D3");
var changeStitch = stitchCount.getValue();
var rowCount = sheet.getRange("B2:B3");
var changeRow = rowCount.getValue();
var stitchPerRow = sheet.getRange("D5");
var valueSPR = stitchPerRow.getValue();
var rowComplete = sheet.getRange("D6");
var valueRowC = rowComplete.getValue();
var stitchZero = changeStitch - valueSPR;
var userIncrement = sheet.getRange("F12:G12");
var valueUserNum = userIncrement.getValue();
var rowsToAddIfSmallSPR = Math.round(10/valueSPR);
var stitchRolloverIfSmallSPR = (10 % valueSPR);
function resetAll() {
stitchCount.clearContent();
rowCount.clearContent();
stitchPerRow.clearContent();
rowComplete.clearContent();
userIncrement.clearContent();
}
function zeroStitch() {
stitchCount.setValue("0")
}
function zeroRow() {
rowCount.setValue("0")
}
function add1Stitch() { //one stitch
checkForSPR(); //get user input if SPR isn't set
if ((changeStitch + 1) === valueSPR) { //if stitchCount completes a row with the additonal stitch
zeroStitch(); //zero out Stitches
add1Row(); // add a row
} else if (changeStitch > valueSPR){ //if stitchCount completes a row with leftovers
stitchCount.setValue(changeStitch - valueSPR); //figure out leftovers-->stitchCount
add1Row(); // add a row
} else {
stitchCount.setValue(changeStitch+1); //if row not complete with additional stitch simply add 1 stitch
}
}
function add2Stitches() { //two stitches
checkForSPR(); //get user input if SPR isn't set
if ((changeStitch + 2) === valueSPR) { //if stitchCount completes row w/ additional 2 stitches
zeroStitch(); //zero out stitches
add1Row(); //add a row
} else if ((changeStitch +2) > valueSPR) { //if stitchCount completes row w/ leftovers
stitchCount.setValue((changeStitch +2) - valueSPR); //figure out leftovers-->stitchCount
add1Row(); // add a row
} else {
stitchCount.setValue(changeStitch+2); //if row not complete with additional stitches simply add 2 stitches
}
}
function add10Stitches() {
checkForSPR();
if ((changeStitch +10) > valueSPR) {
stitchCount.setValue((changeStitch +10) - valueSPR);
checkSPRLessThan10();
} else if ((changeStitch + 10) === valueSPR) {
zeroStitch();
checkSPRLessThan10();
} else {
stitchCount.setValue(changeStitch+10);
}
}
function checkSPRLessThan10() {
if(valueSPR < 10) {
stitchCount.setValue(changeStitch + stitchRolloverIfSmallSPR);
rowCount.setValue(changeRow + rowsToAddIfSmallSPR);
} else {
add1Row();
}
}
function addStitchUserNum() {
checkForSPR();
if ((changeStitch + valueUserNum) === valueSPR) {
zeroStitch();
add1Row();
} else if (changeStitch > valueSPR){
stitchCount.setValue(changeStitch - valueSPR);
add1Row();
} else {
stitchCount.setValue(changeStitch+valueUserNum);
}
}
function addRowUserNum() {
rowCount.setValue(changeRow+valueUserNum)
}
function add1Row() {
rowCount.setValue(changeRow+1);
checkForRC();
}
function sub1Stitch() {
stitchCount.setValue(changeStitch-1)
}
function sub1Row() {
rowCount.setValue(changeRow-1);
}
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Custom Menu')
.addItem('Show prompt', 'showPrompt')
.addToUi();
}
function inputStitchPerRow() {
var ui = SpreadsheetApp.getUi();
var result = ui.prompt(
'Stitches per Row',
'Please enter the number of stitches needed to complete a row',
ui.ButtonSet.OK_CANCEL);
// Process the user's response.
var button = result.getSelectedButton();
var text = result.getResponseText();
if (button == ui.Button.OK) {
// User clicked "OK".
stitchPerRow.setValue(text);
} else if (button == ui.Button.CANCEL) {
// User clicked "Cancel".
ui.alert('You didn\'t set your stitches per row, please set manually');
} else if (button == ui.Button.CLOSE) {
// User clicked X in the title bar.
ui.alert('Please set stitches per row manually');
}
}
function checkForSPR() {
if (valueSPR == "") {
inputStitchPerRow();
}
}
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.createMenu('Custom Menu')
.addItem('Show prompt', 'showPrompt')
.addToUi();
}
function inputRowComplete() {
var ui = SpreadsheetApp.getUi();
var response = ui.prompt(
'Rows to Complete Project',
'Please enter the number of rows needed to complete your project',
ui.ButtonSet.OK_CANCEL);
// Process the user's response.
if (response.getSelectedButton() == ui.Button.OK) {
rowComplete.setValue(response.getResponseText());
} else if (response.getSelectedButton() == ui.Button.CANCEL) {
ui.alert('Please set your Rows to Completion');
} else {
ui.alert('Please set your Rows to Completion');
}
}
function checkForRC() {
if(valueRowC == "") {
inputRowComplete();
}
}
如果我能更清楚,或者我在错误的地方问这个问题,请告诉我。在此先感谢您的帮助!
答案 0 :(得分:0)
您可以将代码合并到:
function add1Stitch() {
addStitches(1);
}
function add2Stitches() { //two stitches
addStitches(2);
}
function add10Stitches() {
addStitches(10);
}
function addStitches(numberOfStichesToAdd) {
checkForSPR(); //get user input if SPR isn't set
Logger.log('changeStitch: ' + changeStitch)
Logger.log('valueSPR: ' + valueSPR)
if ((changeStitch + numberOfStichesToAdd) === valueSPR) { //if stitchCount completes a row with the additonal stitch
zeroStitch(); //zero out Stitches
add1Row(); // add a row
if (numberOfStichesToAdd === 10) {
checkSPRLessThan10();
}
} else if ((changeStitch + numberOfStichesToAdd) > valueSPR){ //if stitchCount completes a row with leftovers
stitchCount.setValue((changeStitch + numberOfStichesToAdd) - valueSPR); //figure out leftovers-->stitchCount
add1Row(); // add a row
} else {
stitchCount.setValue(changeStitch + numberOfStichesToAdd); //if row not complete with additional stitch simply add 1 stitch
}
}
运行代码,然后使用&#34;查看&#34;菜单查看&#34; Logs&#34;在任何情况下,您都需要通过使用Logger.log()
我们不知道电子表格中的值和被检索的值。如果你能提供关键变量的值,那将会有所帮助。