|
Answer» I am trying to write some code to edit a single cell of a .csv file. I have it to where it will change the cell, but it will skip writing the empty cells. Any ideas how I can fix this? I believe it has to do with the WAY the FOR loop operates.
Code: [Select]echo off setlocal EnableDelayedExpansion
set file=. set "target=%~1" set "file=%~2" set "change=%~3"
echo. if not defined file call :error "No Target" && exit /b if not defined target call :error "No Cell Defined" && exit /b if not defined change call :error "No Change Declared" && exit /b if exist %file% ( if exist %file%.new.csv del %file%.new.csv ) ELSE ( call :error "No Target" exit /b )
set "alpha=A/1 B/2 C/3 D/4 E/5 F/6 G/7 H/8 I/9 J/10 K/11 L/12 M/13 N/14 O/15 P/16 Q/17 R/18 S/19 T/20 U/21 V/22
W/23 X/24 Y/25 Z/26"
set a=1 :l set /a a+=1 set /a c=%a%-1 set /a b=!target:~%c%,%a%!+0 2>nul || goto :error if "%b%"=="0" goto :l
set row=!target:~0,%c%! set col=!target:~%c%!
set b=0 set row_num=0 :a set c=!row:~%b%,1! for %%A in (%alpha%) do ( for /f "tokens=1,2 delims=/" %%B in ("%%A") do ( if "%%B"=="!c!" set row_tmp=!row_tmp!%%C\ if "%%B"=="!c!" set /a row_num+=1 ) ) set /a b+=1 if not "!row:~%b%,1!"=="" goto :a
set row_score=0 set /a row_num-=1 set row_tmp=%row_tmp:\= % for %%A in (%row_tmp%) do ( set row_val=1 for /l %%G in (1,1,!row_num!) do set /a row_val*=26 set /a row_val*=%%A set /a row_num-=1 set /a row_score+=!row_val! )
set row=%col% set col=%row_score%
set row_counter=0 for /f "delims=" %%A in (%file%) do ( set AA=%%A set "AA=!AA:;=, ,!" set /a row_counter+=1 if "!row_counter!"=="%row%" ( set a=0 for %%B in (!AA!) do ( set /a a+=1 if "!a!"=="%col%" ( 0>nul set /p"=%change%," 1>>%file%.new.csv ) else ( 0>nul set /p"=%%B," 1>>%file%.new.csv ) ) echo. 1>>%file%.new.csv ) else ( echo !AA! 1>>%file%.new.csv ) )
REM ====== Start Debug ===== REM show file echo Old File for /f "delims=" %%A in (%file%) do echo %%A echo. echo New File for /f "delims=" %%A in (%file%.new.csv) do echo %%A
exit /b
:error echo [ERROR] %~1
Code: (Demo) [Select] T:\spreadsheets>spreadsheet A1 test2.csv "Testing"
Old File 1;;;1
New File Testing,1,
T:\spreadsheets> It could be in this line - commas and semicolons and equals etc are treated as whitespace in a for in do command.
You seem to be passing terms like CommaSpaceComma and that will just vanish in the for in do and be treated like a single space, unless it is double QUOTED.
Code: [Select]for %%B in (!AA!) do ( VBscript using the Excel object has been mentioned, and GAWK is probably a good choice too.
|