2017-04-01 4 views
0

私は入力としてかなりの量の数字を持っているMSスモールベーシックプログラムを計画しています。エクセルシートからナンバーテーブルをプログラムに読み込むにはどうすればいいですか? アンティMSスモールベーシック:エクセルシートからナンバーテーブルを入力

+0

Small Basicはこれのための言語ではありません。スプレッドシートに直接読み込ませる機能はありません。CSVファイルとして保存し、読み込んだデータを手動で解析する必要があります。また、Small Basicのアレイシステムは使いやすさのために設計されており、結果として痛みを伴います。このプロジェクトではVisual BasicやPythonを調べることをお勧めします。 – codingCat

+0

Litdevの拡張機能には、これを助けるツールがいくつかあると思います。 – Zock77

答えて

0

ザ・ハード・ウェイ -

プログラミングについての大部分は、ほぼすべての言語はほぼ何でもできるということです。 Small Basicも例外ではありません。あなたが他の言語と比較して巨大な時間を覚えていても気にしないならば、あなたはファイルで何かできることができます。

ポイントを証明するために、Small Basicの2次元配列にExcelテーブルを読み込むサンプルを作成しました。

スモール・ベーシックにExcelファイルをインポートするには、最初に行う必要があるのは、扱いやすい形式でファイルを保存することです。以下の例では、ファイルがカンマ区切りのCSV形式で保存されていることを前提としています。この形式に従って、各セルはコンマで終了し、各行はCR-LFコンボで終了します。残りの部分は文字単位で情報文字を解析するだけです。

'Reading an Excel Table saved as a CSV (comma seperated) file. 
TextWindow.Show() 

LoadFile() 
TextWindow.WriteLine("File Size = " + Text.GetLength(DataIn)) 
ParseFile() 
TextWindow.WriteLine("Rows = " + rows + ", Columns = " + cols) 
DisplayTable() 

'--------------------------------------------------------------- 

'Read the contents of the CSV file into memory 
Sub LoadFile 
    filename = Program.Directory 
    filename = filename + "\excelintoSB.csv" 

    DataIn = File.ReadContents(filename) 
EndSub 

'Parse the file contents, looking for commas and line breaks to separate the cells. 
Sub ParseFile 
    row = 1 
    col = 1 
    cell = "" 

    For i =1 To Text.GetLength(DataIn) 'Look at each character in the file 
    ch = Text.GetSubText(DataIn,i,1) 
    'Is it a comma or a cariage return? Store the Cell 
    If ch = "," or text.GetCharacterCode(ch) = 13 then 
     table[row][col] = cell 
     cell = "" 
     If text.GetCharacterCode(ch) = 13 Then 'end of row, start a new one 
     row = row + 1 
     col = 1 
     Else 'New Cell, current row 
     col = col + 1 
     If col > maxCol then 'Keep track of how many columns we have encountered 
      maxCol = col 
     endif 
     endif 
    ElseIf text.GetCharacterCode(ch) <> 10 then 'build the cell, ignoring line feeds. 
     cell = cell + ch 
    EndIf 
    EndFor 
    rows = row - 1 
    cols = maxCol 
EndSub 

'Display the table in row/column format 
Sub DisplayTable 
    TextWindow.WriteLine("The Table --- ") 
    For i = 1 To rows 
    TextWindow.Write("[ ") 
    For j = 1 To cols 
     TextWindow.Write(table[i][j] + " ") 
    EndFor 
    TextWindow.WriteLine("]") 
    EndFor 
EndSub 

次は、私はそれをテストしたファイルであるExcelで最初に作成し、CSVとして保存:

 
1a,1b,1c,1d 
2a,2b,2c,2d 
3a,3b,3c,3d 

をお楽しみください!

関連する問題