Frage

I have a Google spreadsheet file on Google Drive. Google Drive Web has the possibility of uploading an Excel file and importing it into the existing file as a new worksheet.

I want to do the same by using the Google Drive or Google Spreadsheet APIs, but I didn't find how to do it with the API documentation.

I know how to upload an Excel file and convert it to a Google spreadsheet using the google_drive gem, but how do I combine two spreadsheets into one?

UPD: if someone will look for the answer on this question I created Google-Apps-Script as @eddyparkinson suggested

// Script-as-app template.
function doGet(e) {
  if (typeof e.parameter.src === 'undefined' || !e.parameter.dst === 'undefined') {
    return result({ status: 404, message: 'src or dst parameter is not defined. Use next format: ?src=key&dst=key[&src_sheet=0 or sheet name]' });
  }

  Logger.log('src is "%s", dst is "%s"', e.parameter.src, e.parameter.dst);

  var source = SpreadsheetApp.openById(e.parameter.src);
  if (typeof source === 'undefined') {
    return result({ status: 404, message: 'The source spreadsheet (key: "'+ e.parameter.src +'") has not been found.' });
  }

  var destination = SpreadsheetApp.openById(e.parameter.dst);
  if (typeof destination === 'undefined') {
    return result({ status: 404, message: 'The destination spreadsheet (key: "'+ e.parameter.dst +'") has not been found.' });
  }

  var sourceSheet;
  if (typeof e.parameter.src_sheet !== 'undefined') {
    Logger.log('src_sheet is "%s"', e.parameter.src_sheet);

    var sheetIndex = parseInt(e.parameter.src_sheet);
    Logger.log('sheetIndex is %s', sheetIndex);

    if (isNaN(sheetIndex)) {
      Logger.log('getSheetByName');      
      sourceSheet = source.getSheetByName(e.parameter.src_sheet);
      if (typeof sourceSheet === 'undefined') {
         return result({ status: 404, message: 'The src_sheet name is not exist in the source spreadsheet.' });
      }
    }
    else {
      var sheets = source.getSheets()
      if (sheetIndex >= sheets.length) {
        return result({ status: 404, message: 'The src_sheet index is out of range of sheets.' });
      }
      sourceSheet = sheets[sheetIndex];
    }
  }
  else {
    sourceSheet = source.getSheets()[0];
  }  

  sourceSheet.copyTo(destination);  

  return result({ status: 200 });
}

var resultData;
function result(value) {
  resultData = value;  
  return HtmlService.createTemplateFromFile('status').evaluate();
}

where I wait for src and dst keys of spreadsheets.

In ruby I use google_drive gem to upload files and call Google App Scripts

  def self.upload_to_google_drive()
    session = GoogleDrive.login('email', 'password')
    f1 = session.upload_from_file('parsed/xlsx/#CAR06022540.xlsx', "#CAR06022540")
    f2 = session.upload_from_file('parsed/xlsx/#CAR06031629.xlsx', "#CAR06031629")
    url = "https://script.google.com/macros/s/AKfycbzOeZJ4LuvBxFqyi1WDpzNiKbhkjp33D9yWTOTLlq3g56sCqJ_e/exec?src=#{f1.key}&dst=#{f2.key}"
    # call Google-apps-script 
    result = session.request(:get, url)
    puts result
  end
War es hilfreich?

Lösung

Copy to another spreadsheet option:

1) Google-apps-script will copy it: https://developers.google.com/apps-script/reference/spreadsheet/sheet#copyTo(Spreadsheet)

2) The only option with google-spreadsheet-api looks to be to a manual copy. So, load in the sheet(s) you want to copy, and create new sheet(s) in the spreadsheet saving the copied values. i.e. there is nothing in the docs https://developers.google.com/google-apps/spreadsheets/ or examples (ListDemo.java or http://gdata-java-client.googlecode.com/svn-history/r51/trunk/java/sample/spreadsheet/cell/CellDemo.java) that will do the copy for you.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top