否则,如果在谷歌脚本

时间:2016-03-27 19:53:26

标签: javascript if-statement google-sheets

我正在尝试搜索一系列单元格,我已经设置了用于过滤的公式。我正在尝试确定单元格可能具有的三个不同值,并根据单元格匹配的值使用function setFormulas(){ var ss = SpreadsheetApp.getActive() var sheet = SpreadsheetApp.getActiveSheet() var cell = ss.getActiveCell() var cell1 = ("C2"); var formulaCell = ("A5"); var cell2 = ("C3"); var cell1isblank = SpreadsheetApp.getActiveSheet().getRange(cell1).isBlank() var cell2isblank = SpreadsheetApp.getActiveSheet().getRange(cell2).isBlank() if (cell1 == "0" ) { SpreadsheetApp.getActiveSheet().getRange(formulaCell).setFormula("formula1") } else if (cell2 == "0" ) { 2SpreadsheetApp.getActiveSheet().getRange(formulaCell).setFormula("formula2") } } else { SpreadsheetApp.getActiveSheet().getRange(formulaCell).setFormula("Formula3") } 。她是我到目前为止所提出的。它适用于前两个公式,但如果两个单元格都包含数据,则不设置“公式3”。

import java.util.List;
import java.util.ArrayList;

/**
 * The Deck class represents a shuffled deck of cards.
 * It provides several operations including
 *      initialize, shuffle, deal, and check if empty.
*/
public class Deck 
{
/**
 * cards contains all the cards in the deck.
 */
public static List<Card> cards;

/**
 * size is the number of not-yet-dealt cards.
 * Cards are dealt from the top (highest index) down.
 * The next card to be dealt is at size - 1.
 */
private int size;
public static Card cardOne;

/**
 * Creates a new <code>Deck</code> instance.<BR>
 * It pairs each element of ranks with each element of suits,
 * and produces one of the corresponding card.
 * @param ranks is an array containing all of the card ranks.
 * @param suits is an array containing all of the card suits.
 * @param values is an array containing all of the card point values.
 */
public Deck(String[] ranks, String[] suits, int[] values) 
{
    for(int i=0; i<13;i++)
    {
        suits[i] = "Heart";
        ranks[i] = cardOne.rank();
        values[i] = cardOne.pointValue();
        cards.add(cardOne);
    }
    for(int i=0; i<13;i++)
    {
        suits[i] = "Spade";
        ranks[i] = cardOne.rank();
        values[i] = cardOne.pointValue();
        cards.add(cardOne);
    }
    for(int i=0; i<13;i++)
    {
        suits[i] = "Club";
        ranks[i] = cardOne.rank();
        values[i] = cardOne.pointValue();
        cards.add(cardOne);
    }
    for(int i=0; i<13;i++)
    {
        suits[i] = "Diamond";
        ranks[i] = cardOne.rank();
        values[i] = cardOne.pointValue();
        cards.add(cardOne);
    }

}


/**
 * Determines if this deck is empty (no undealt cards).
 * @return true if this deck is empty, false otherwise.
 */
public static boolean isEmpty() 
{
    if(cards.size()==0)
        return true;
    else
        return false;
}

/**
 * Accesses the number of undealt cards in this deck.
 * @return the number of undealt cards in this deck.
 */
public static int size() 
{
    return  cards.size();
}

/**
 * Randomly permute the given collection of cards
 * and reset the size to represent the entire deck.
 */
public static List<Card> Shuffled[];
public void shuffle() 
{
    for(int i=0; i<52; i++)
    {
        cards.get(i);

        int k=(int)(Math.random()*100);
        while(k >52 || k<0)
        {
            k=(int)(Math.random()*100);
        }
        if(Shuffled[k]==null)
            Shuffled[k]=(List<Card>) cards.get(i);
    }

}

/**
 * Deals a card from this deck.
 * @return the card just dealt, or null if all the cards have been
 *         previously dealt.
 */
public Card deal() 
{
    int cardDealed= (int)(Math.random()*100);
    while(cardDealed >52 || cardDealed<0)
    {
        cardDealed=(int)(Math.random()*100);
    }
    Shuffled[cardDealed].remove(cardDealed);

    return (Card) Shuffled[cardDealed];
}

/**
 * Generates and returns a string representation of this deck.
 * @return a string representation of this deck.
 */
@Override
public String toString() 
{
    String rtn = "size = " + size + "\nUndealt cards: \n";

    for (int k = size - 1; k >= 0; k--) {
        rtn = rtn + cards.get(k);
        if (k != 0) {
            rtn = rtn + ", ";
        }
        if ((size - k) % 2 == 0) {
            // Insert carriage returns so entire deck is visible on console.
            rtn = rtn + "\n";
        }
    }

    rtn = rtn + "\nDealt cards: \n";
    for (int k = cards.size() - 1; k >= size; k--) {
        rtn = rtn + cards.get(k);
        if (k != size) {
            rtn = rtn + ", ";
        }
        if ((k - cards.size()) % 2 == 0) {
            // Insert carriage returns so entire deck is visible on console.
            rtn = rtn + "\n";
        }
    }

    rtn = rtn + "\n";
    return rtn;
   }
}

1 个答案:

答案 0 :(得分:2)

拳头,你有一个错误的支架。其次,您需要对cellisblank变量进行真/假比较。此外,还有一个拼写错误(2SpreadsheetApp)。试试这个:

function setFormulas(){
   var ss = SpreadsheetApp.getActive()          
   var sheet = SpreadsheetApp.getActiveSheet()
   var cell = ss.getActiveCell()
   var cell1 = ("C2");
   var formulaCell = ("A5");
   var cell2 = ("C3");
   var cell1isblank = SpreadsheetApp.getActiveSheet().getRange(cell1).isBlank()
   var cell2isblank = SpreadsheetApp.getActiveSheet().getRange(cell2).isBlank()

    if (cell1isblank == false && cell2isblank == true) {
  SpreadsheetApp.getActiveSheet().getRange(formulaCell).setFormula("=formula1")
    }
    else if (cell2isblank == false && cell1isblank == true ) {
SpreadsheetApp.getActiveSheet().getRange(formulaCell).setFormula("=formula2")
    }
  //}
   else {
  SpreadsheetApp.getActiveSheet().getRange(formulaCell).setFormula("=Formula3")
  }
}  
相关问题