0

I have a problem with google app script I was uploading my files to drive using php, The file is inserting in to drive successfully. But when i want to edit my document to highlight the words using google app script I am getting error, The File Id of google drive and google docs are different, So I am unable to edit the file in google docs using app script. Please look in to my code and suggest me

I am using google docs link to edit but the file id I have with me is drive file id, So suggest me how can i get that

   function doGet(param){
            var files = DriveApp.getFilesByName('11008.doc');
            while (files.hasNext()) {
                var file = files.next();
                var url = file.getUrl();
                url = url.replace("?usp=drivesdk","");
                Logger.log(url);
                var doc = DocumentApp.openByUrl('https://docs.google.com/document/d/18CEwLeTLwsQ_xR2BLWqs5E1lQrd9SsNuUwlnBpWLQro/edit');
                var textToHighlight = 'MA';
                var highlightStyle = {};
                highlightStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FF0000';
                var paras = doc.getParagraphs();
                var textLocation = {};
                var i;

                for (i=0; i<paras.length; ++i) {
                    textLocation = paras[i].findText(textToHighlight);
                    if (textLocation != null && textLocation.getStartOffset() != -1) {
                        textLocation.getElement().setAttributes(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(), highlightStyle);
                    }
                }
                return doc;
            } 
        }  
Kara
  • 6,115
  • 16
  • 50
  • 57
Dsvarma M
  • 1
  • 1
  • Here is the easiest way to get file ID from url. Use regex, see sample in this [SO post](https://stackoverflow.com/questions/16840038/easiest-way-to-get-file-id-from-url-on-google-apps-script). Also you can check out [this discussion from google help forum](https://productforums.google.com/forum/#!topic/docs/3STOEukh1pU) for further information. – MαπμQμαπkγVπ.0 May 17 '18 at 11:33

1 Answers1

0

Perhaps this will help:

function highLightText(txt,from) {
  var highlight={};
  highlight[DocumentApp.Attribute.BACKGROUND_COLOR]='#ff0000';
  var from=from || '';
  var txt=txt || '';
  var doc=DocumentApp.getActiveDocument();
  var body=doc.getBody();
  if(from){
    var rgel=body.findText(txt, from);
  }else{
    var rgel=body.findText(txt);
  }
  if(rgel){
    var el=rgel.getElement();
    var start=rgel.getStartOffset();
    var end=rgel.getEndOffsetInclusive();
    if(rgel.isPartial()){
      el.asText().setAttributes(start, end, highlight)
    }else{
      el.asText().setAttributes(highlight);
    }
    highLightText(txt,rgel);
  }
}
Cooper
  • 59,616
  • 6
  • 23
  • 54