[google apps script]๊ตฌ๊ธ€์Šคํ”„๋ ˆ๋“œ์‹œํŠธ๋ฅผ ๋‚ด PC ํŠน์ • ํด๋”์— ์ฃผ๊ธฐ์ ์œผ๋กœ ์—‘์…€ํŒŒ์ผ๋กœ ๋‹ค์šด ๋ฐ›๊ธฐ
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋””์ง€ํ„ธ ๊ฟ€ํŒ

[google apps script]๊ตฌ๊ธ€์Šคํ”„๋ ˆ๋“œ์‹œํŠธ๋ฅผ ๋‚ด PC ํŠน์ • ํด๋”์— ์ฃผ๊ธฐ์ ์œผ๋กœ ์—‘์…€ํŒŒ์ผ๋กœ ๋‹ค์šด ๋ฐ›๊ธฐ

by ์ผ์ƒ์„ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค 2022. 6. 30.

๊ตฌ๊ธ€์Šคํ”„๋ ˆ๋“œ์‹œํŠธ ํŒŒ์ผ์„ ๋ณธ์ธ PC ํŠน์ • ํด๋”์— ์ €์žฅํ•˜๊ณ  ์‹ถ๋‹ค๋Š” ๋ฌธ์˜๊ฐ€ ๋“ค์–ด์™€ ์—ด์‹ฌํžˆ ์ƒ๊ฐํ•ด๋ดค์ง€๋งŒ ์–ด๋ ค์›Œ ์•ฝ๊ฐ„ ํŽธ๋ฒ•์œผ๋กœ ๋งŒ๋“ค์–ด๋ดค๋‹ค. ์กฐ๊ฑด์ด ์›๋ž˜๋Š” ๊ตฌ๊ธ€์Šคํ”„๋ ˆ๋“œ์‹œํŠธ์˜ ์ž‘์„ฑ๋œ ํ–‰๊นŒ์ง€๋งŒ ๋‚˜์˜ค๊ฒŒ ํ•ด๋‹ฌ๋ผ๊ณ  ํ•˜์˜€์ง€๋งŒ ์šฐ์„  ๋‚ด ์„ ์—์„œ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜์˜€๋‹ค. 

์•„๋ž˜๋Š” ๋‹ค์šด๋ฐ›๋Š” ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ ํŒŒ์ผ์˜ ๋ช…์„ ์‹คํ–‰๋˜๋Š” ๋‚ ์งœ๋ฅผ ๋ถ™์—ฌ์„œ ๋‹ค๋ฅด๊ฒŒ ํ•˜๊ณ , ํ•ด๋‹น ํŒŒ์ผ์„ ํŠน์ • ํด๋”๋กœ ์ด๋™์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

 

 

1. google apps script ์†Œ์Šค (code.js)

function doGet(e){
  return createDataUrl();
}

function createDataUrl() {
  var type = "xlsx";
  var date = Utilities.formatDate(new Date(),"GMT+0900","yyyy-MM-dd HH:mm:ss");

  date = date.replaceAll("-","");
  date = date.substring(0,9);


  const mimeTypes = { xlsx: MimeType.MICROSOFT_EXCEL};
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const url = `https://docs.google.com/spreadsheets/export?exportFormat=${type}&id=์Šคํ”„๋ ˆ๋“œ์‹œํŠธID์ž…๋ ฅ`;
  var blob = UrlFetchApp.fetch(url, {
    headers: { authorization: `Bearer ${ScriptApp.getOAuthToken()}` },
  }).getBlob();

  var html = HtmlService.createTemplate('<body></body><script>const a=document.createElement("a");document.body.appendChild(a),a.download="<?= filename ?>",a.href="<?= data ?>",a.click();</script>');
  html.data = "data:${mimeTypes[type]};base64," + Utilities.base64Encode(blob.getBytes());
  html.filename = `${ss.getName() + String(date)}.${type}`;

  return html.evaluate();
}

 

๋งŒ์•ฝ ์œ„ ์†Œ์Šค์—์„œ ํŒŒ์ผ๋ช…์„ ๋™์ผํ•˜๊ฒŒ ํ•ญ์ƒ ๋‹ค์šด ๋ฐ›๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ date ๋ถ€๋ถ„์„ ๋‹ค ์ง€์›์ฃผ๊ณ  ํŒŒ์ผ์ด๋ฆ„ ์„ค์ •ํ•œ ๋ถ€๋ถ„ ์†Œ์Šค๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค.

html.filename = "๋™์ผํ•œ์ด๋ฆ„.xlsx";

 

 

๋ฐ˜์‘ํ˜•

 

 

2.  html ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ (Index.html)

 

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    
  </body>
</html>

์ž˜ ์‹คํ–‰๋˜๋Š” ์•ก์…˜์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž„์˜๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” html ์ •๋„๋กœ ํ•ด์„ํ–ˆ๋‹ค. (์›น์•ฑ์—์„œ ์‹คํ–‰)

 

 

3.  ์›น ์•ฑ์œผ๋กœ ๋ฐฐํฌ ํ•˜๊ธฐ

์˜ค๋ฅธ์ชฝ ์ƒ๋‹จ์— ์žˆ๋Š” ๋ฐฐํฌ > ์ƒˆ ๋ฐฐํฌ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์›น ์•ฑ์„ ์•ก์„ธ์Šคํ•˜๋Š” ์‚ฌ์šฉ์ž / ๊ตฌ๊ธ€ ๊ณ„์ •์ด ์žˆ๋Š” ๋ชจ๋“  ์‚ฌ์šฉ์ž๋ฅผ ์„ ํƒํ•˜๊ณ  ๋ฐฐํฌ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ URL์ด ์ƒ์„ฑ๋œ๋‹ค. ์ƒ์„ฑ๋œ URL์€ ๋ณต์‚ฌํ•˜์—ฌ ์•„๋ž˜ ์Šคํฌ๋ฆฝํŠธ์— ์‚ฌ์šฉํ•œ๋‹ค.

 

 

4. ์œˆ๋„์šฐ PC์— ํŒŒ์ผ ์ด๋™ํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ ๋งŒ๋“ค์–ด ์Šค์ผ€์ค„ ์˜ˆ์•ฝํ•˜๊ธฐ

์‚ฌ์ „์— ์•„๋ž˜ ๋งํฌ๋ฅผ ๋ณด๋ฉด .bat ํŒŒ์ผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• ๋ฐ ์ž‘์—… ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ์œ„์— ํŠธ๋ฆฌ๊ฑฐ ์‹คํ–‰ ์‹œ๊ฐ„๊ณผ ๋งž์ถฐ ์ดํ›„ ์‹œ๊ฐ„๋Œ€๋กœ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

[Windows] ์ •ํ•ด์ง„ ์‹œ๊ฐ„์— ํŠน์ •ํŒŒ์ผ ์‹คํ–‰ ํ›„ ๋‹จ์ถ•ํ‚ค ์‹คํ–‰ , ๋งคํฌ๋กœ ์‚ฌ์šฉ ๊ฟ€ํŒ

์ด๋ฒˆ ํฌ์ŠคํŒ…์€ ์ •ํ•ด์ง„ ์‹œ๊ฐ„์— ํŠน์ •ํŒŒ์ผ ์‹คํ–‰ ํ›„ ๋‹จ์ถ•ํ‚ค๋„ ์ž๋™์œผ๋กœ ์ž…๋ ฅํ•˜๋Š” ๋งคํฌ๋กœ ์‚ฌ์šฉ์„ ๋” ํšจ์œจ์ ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฟ€ํŒ์„ ๊ณต์œ ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ ๋ฐฉ๋ฒ•๊ณผ ์œˆ๋„์šฐ์—์„œ ์‹คํ–‰ํ•˜๋Š”

odaily.tistory.com

๋ฐฐ์น˜ ํŒŒ์ผ ์†Œ์Šค (.bat)

@echo off

::์›นํŽ˜์ด์ง€ ์‹คํ–‰
 start "์œ„์—์„œ ๋ณต์‚ฌํ•œ ์›น ์•ฑ URL"

:: ํŒŒ์ผ ๋‹ค์šด์— ํ•„์š”ํ•œ ์‹œ๊ฐ„์œผ๋กœ ์„ค์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Œ (๋„‰๋„‰ํ•˜๊ฒŒ)
timeout 10

::ํŒŒ์ผ๋ช…์— ๋‚ ์งœ๊ตฌ๋ถ„์„ ์œ„ํ•œ
SET yyyymmdd=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%
REM SET yyyymmdd=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%

:: FILENAME
SET filePath=C:\excelFileTest%yyyymmdd%.xlsx

echo %filePath% ํŒŒ์ผ ์ด๋™

:: file ํด๋” ์ด๋™ (์•ž์ด ๋‹ค์šด๋ฐ›์€ ํด๋”, ๋’ค๊ฐ€ ์˜ฎ๊ธธ ํด๋”)
move %filePath% C:\test


::KEY ์ข…๋ฃŒ, pause ์ผ ๊ฒฝ์šฐ ๋„์Šค ์ฐฝ์ด ๋‚จ์œผ๋ฏ€๋กœ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰์ด ๋˜๋Š”๊ฒŒ ํ™•์ธ์ด ๋œ๋‹ค๋ฉด exit
pause

๊ตฌ๊ธ€ ์•ฑ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋งŒํผ, ๊ผญ ์ธํ„ฐ๋„ท ๊ธฐ๋ณธ ์•ฑ ์„ค์ •์€ chrome์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์–ด์•ผ ํ•˜๋ฉฐ ๋ณธ์ธ id๊ฐ€ ๋กœ๊ทธ์ธ ๋˜์–ด์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ ํŒŒ์ผ ์ด๋™์ด ๋œ ํ›„ ๋„์–ด์ง„ ์›นํŽ˜์ด์ง€๋ฅผ ์ข…๋ฃŒ ํ•ด์•ผ ํ•œ๋‹ค๋ฉด pause ์ „์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

๋‹จ์ถ•ํ‚ค ๋ณด๋‚ด๊ธฐ (.bat)

::ํŒŒ์ผ์ด ์ €์žฅ๋  ์œ„์น˜์™€ ์ด๋ฆ„์„ ๋ณ€์ˆ˜๋กœ ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค::ํŒŒ์ผ ์ด๋ฆ„์€ snedkey.vbs์ž…๋‹ˆ๋‹ค.
set file="%temp%\sendkey.vbs"

::vbs ํŒŒ์ผ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
(echo Set objShell = WScript.CreateObject("WScript.Shell"^)
echo objShell.SendKeys "%{F4}") > %file%

::๋งŒ๋“  vbs ํŒŒ์ผ์„ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.
::vbs ํŒŒ์ผ์€ wscript๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
wscript %file% | more

::๋งŒ๋“ค์—ˆ๋˜ vbsํŒŒ์ผ์„ ์‚ญ์ œ ํ•ฉ๋‹ˆ๋‹ค.
del /q %file%

 

๊ฒฐ๊ณผ

์ด๋ ‡๊ฒŒ ํ•  ๊ฒฝ์šฐ ํŒŒ์ผ์ด ์ด๋™๋˜๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค. 6์›” ์ดˆ์— ๋ฌธ์˜ ๋“ค์–ด์™”๋Š”๋ฐ ๋‚˜๋ฆ„ ์‰ฝ๊ฒŒ ํ•ด๋ณด๋ คํ–ˆ๋Š”๋ฐ ์›ํ•˜๋˜ ๋ฐฉํ–ฅ๋Œ€๋กœ ์•ˆ๋˜์„œ ๋„ˆ๋ฌด ์ŠฌํŽ๋˜ ์ž‘์—…์ด๋‹ค. ๊ทธ๋ž˜๋„ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‚ด๊ฐ€ ์ฃผ๊ธฐ์ ์œผ๋กœ ํŒŒ์ผ์„ ๋‹ค์šด๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋œ ์‹œ๊ฐ„์ด์—ˆ๋‹ค.

 

๋Œ“๊ธ€