brunch

You can make anything
by writing

C.S.Lewis

by 윤종윤 Jan 31. 2023

스프레드시트와 구글슬라이드 연동

스프레드시트 데이터를 구글 슬라이드 페이지마다 등록하기

회사 업무 중 400명 정도의 학생의 수료증을 제작해야 하는 과업이 생겼다. 

400개의 학생 정보를 일일이 수료증 템플릿에 입력해야 하는 귀찮은 작업이라, 쉽게 처리할 방법은 없을까 고민하다다 찾은 스프레드시트의 apps script 기능을 발견하고 업무를 끝냈다. 

하마터면 하루 종일 단순 반복 작업을 하고 있을 뻔했다. 



구글 스프레드 시트 데이터 등록해 두기

구글슬라이드에 입력될 데이터는 구글스프레드시트에 입력해 두어야 합니다. 

구글슬라이드와 연동된 후 각 데이터에 대한 슬라이드페이지 url 가 스프레드시트에 자동 등록 됩니다. 

url 등록을 원하는 열에 "URL"을 입력해 놓으면 편리합니다. 

또한 구글 슬라이드에 연동할 데이터의 열이름은 영어로 작성해 주는 것이 좋습니다. 



구글 슬라이드 템플릿 만들어 두기

구글스프레드시트에 있는 값을 등록할 구글 슬라이드를 만들어 둡니다. 

구글 공식 문서에서 알려준 방식을 그대로 사용하기 위해서는 아래 순서를 정확하게 지켜야 합니다. 


1. 기본 표지 화면을 만들어 둔다. 

표지 화면에 특정 값을 넣을 필요는 없습니다. 첫 표지가 없으면 자동 연동이 되지 않았습니다. 

저는 수료증을 만들어야 했기 때문에 수료증 양식을 첫 표지에 입력해 두었습니다. 


2. 구글스프레드시트와 연동할 조건 값을 넣는다.

구글 슬라이드 두 번째 페이지부터 조건값을 넣어줍니다. 

저는 수강생의 이름, 닉네임, 연락처 정보가 필요하였기 때문에 3개의 항목에 대해 아래와 같이 조건설정을 하였습니다. 각 조건 명칭은 스프레드시트의 열 이름과 동일하게 설정해 주어야 합니다. 

- 이름 : {{Name}}

- 닉네임 : {{Nickname}}

- 연락처 : {{phone}}




스프레드시트 데이터 슬라이드 자동 입력 스크립트 세팅

개발자가 아니어도 상관없습니다. 구글 공식문서에서 제공한 스크립트를 복사해서 붙여 넣고 몇 가지 값만 변경해 두면 자동으로 슬라이드 생성이 완료됩니다. 

스프레드시트 가이드 Step 3에 스크립트 복사하기 


1. 아래 스크립트를 복사합니다. 

// Replace <INSERT_SLIDE_DECK_ID> wih the ID of your 
// Google Slides presentation.
let masterDeckID = "<INSERT_SLIDE_DECK_ID>";

// Open the presentation and get the slides in it.
let deck = SlidesApp.openById(masterDeckID);
let slides = deck.getSlides();

// The 2nd slide is the template that will be duplicated
// once per row in the spreadsheet.
let masterSlide = slides[1];

// Load data from the spreadsheet.
let dataRange = SpreadsheetApp.getActive().getDataRange();
let sheetContents = dataRange.getValues();

// Save the header in a variable called header
let header = sheetContents.shift();

// Create an array to save the data to be written back to the sheet.
// We'll use this array to save links to the slides that are created.
let updatedContents = [];

// Reverse the order of rows because new slides will
// be inserted at the top. Without this, the order of slides
// will be the inverse of the ordering of rows in the sheet. 
sheetContents.reverse();

// For every row, create a new slide by duplicating the master slide
// and replace the template variables with data from that row.
sheetContents.forEach(function (row) {

    // Insert a new slide by duplicating the master slide.
    let slide = masterSlide.duplicate();

    // Populate data in the slide that was created
    slide.replaceAllText("{{firstName}}", row[0]);
    slide.replaceAllText("{{lastName}}", row[1]);
    slide.replaceAllText("{{grade}}", row[2]);

    // Create the URL for the slide using the deck's ID and the ID
    // of the slide.
    let slideUrl = `https://docs.google.com/presentation/d/${deck.getId()}/edit#slide=id.${slide.getObjectId()}`;

    // Add this URL to the 4th column of the row and add this row
    // to the data to be written back to the sheet.
    row[3] = slideUrl;
    updatedContents.push(row);
  });

  // Add the header back (remember it was removed using 
  // sheetContents.shift())
  updatedContents.push(header);

  // Reverse the array to preserve the original ordering of 
  // rows in the sheet.
  updatedContents.reverse();

  // Write the updated data back to the Google Sheets spreadsheet.
  dataRange.setValues(updatedContents);

  // Remove the master slide if you no longer need it.
  masterSlide.remove();


2. 스프레드시트의 Apps Script에 접속합니다. 

간혹 Apps Scripts에 접속 시 오류가 뜰 수 있는데, 브라우저 캐시 삭제를 해주면 문제 해결 됩니다. 



3. 복사한 스크립트 붙여 넣기

위에서 복사한 스크립트를 2번째 줄에 붙여 넣기 합니다. 

4. 스크립트 수정하기

기본 세팅은 완료되었고, 앞서 준비한 구글슬라이드 시트와 스프레드시트를 연동하기 위한 추가 세팅 작업이 필요합니다. 

    a. 구글 슬라이드의 고유 ID를 복사해 넣습니다. (주소창에 /d/ 고유id /edit)

        https://docs.google.com/presentation/d/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/edit

    b. 스크립트 4번째 줄 <INSERT_SLIDE_DECK_ID> 부분에 붙여 넣습니다. 

        (" " 사이에 입력해 주어야 합니다)

    c. 스크립트 38번~ 40번에서 조건 값을 입력합니다. 

        저는 이름 {{Name}}, 닉네임{{Nickname}}, 연락처{{Phone}} 으로 설정 한 대로 변경 하였습니다. 

        스프레드시트에서 각각의 조건값이 몇 번째 열인지 함께 입력합니다.

        2번째 열 Name의 경우 row[1], 3번째 열 Nickname의 경우 row[2]

    d. 스크립트 48번에서 생성된 구글 슬라이드 url을 등록할 스프레드시트 열을 입력합니다. 

        저는 스프레드시트 5째 열인 E열에 url을 등록할 것이므로 row[4] 로 입력하였습니다. 


스크립트 저장 후 실행하기

위의 순서대로 스크립트 생성이 완료되면 스크립트를 저장하고 실행 버튼을 눌러 주면 됩니다. 


연동 완료

실행까지 완료하면 구글 슬라이드에 데이터마다 슬라이드 생성이 완료된 것을 확인할 수 있습니다.

구글 스프레드시트에도 생성된 구글슬라이드의 페이지별 url 이 자동 등록된 것을 볼 수 있습니다. 




데이터 개별로 구글 슬라이드에 값을 입력해야 할 경우 유요한 꿀팁을 발견하였습니다. 

앞으로 업무가 조금 더 편해질 것 같습니다. 행복하군요 


작가의 이전글 스타트업의 서비스 기획

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari