function responseWeather() { // : , SMS, ID sms.ru, Google var cityTown= "zlatoust"; var phoneNum="71111111111"; var apiID="111111111111111111111111111111111111"; var docID="11111111111111111111111111111111111111111111"; // , var serverAv = false; while (serverAv == false){ try { DocumentApp.openById(docID); serverAv=true; } catch (e) { Logger.log(' '); } } var docmain = DocumentApp.openById(docID); var bodydoc = docmain.getBody(); bodydoc.clear();// var response = UrlFetchApp.fetch("http://www.wolframalpha.com/input/?i="+cityTown+"+weather"); bodydoc.setText(response); var textEdit = bodydoc.editAsText(); textEdit.deleteText(0, 37280); // , /* . - . - m\s mph . , . */ if (textEdit.findText('°C')!=null){ var degree = '°C'; var convert = false; } else { degree = '°F'; convert = true; } // textEdit.deleteText(0, textEdit.findText(degree).getStartOffset()-5); textEdit.replaceText('</span><span class="info"><dt></dt><dd class="conditions">', ', '); textEdit.replaceText('</dd><dt>', '; '); textEdit.replaceText('wind:</dt><dd>', 'wind: '); textEdit.replaceText('<span> at </span>', ' at '); textEdit.replaceText('humidity:</dt><dd>', 'hum.: '); textEdit.deleteText(textEdit.findText("%").getEndOffsetInclusive()+1, textEdit.findText('" />').getEndOffsetInclusive()); textEdit.replaceText('<span class="high">', ' (buf'); textEdit.replaceText('</span><span class="low">', '...fub'); textEdit.replaceText("</span>", ")</span>"); textEdit.deleteText(textEdit.findText("</span>").getStartOffset(), textEdit.getText().length-1); // - , if (textEdit.findText('>')!=null){ textEdit.deleteText(0, textEdit.findText('>').getEndOffsetInclusive()); } // if (convert==true){ // var buffer1 = textEdit.getText(); textEdit.deleteText(textEdit.findText(degree).getStartOffset(), textEdit.getText().length-1); var len = textEdit.getText().length-1; var buffer2 = Number(textEdit.getText()); bodydoc.clear(); var result = Math.round((buffer2-32)/1.8); textEdit.setText(buffer1); textEdit.deleteText(0, len); textEdit.insertText(0, result); // buffer1 = textEdit.getText(); textEdit.deleteText(0, textEdit.findText("buf").getEndOffsetInclusive()); textEdit.deleteText(textEdit.findText(degree).getStartOffset(), textEdit.getText().length-1); len = textEdit.getText().length-1; buffer2 = Number(textEdit.getText()); bodydoc.clear(); result = Math.round((buffer2-32)/1.8); textEdit.setText(buffer1); textEdit.deleteText(textEdit.findText("buf").getEndOffsetInclusive()+1, textEdit.findText("buf").getEndOffsetInclusive()+1+len); textEdit.insertText(textEdit.findText("buf").getEndOffsetInclusive()+1, result); // buffer1 = textEdit.getText(); textEdit.deleteText(0, textEdit.findText('fub').getEndOffsetInclusive()); textEdit.deleteText(textEdit.findText(degree).getStartOffset(), textEdit.getText().length-1); len = textEdit.getText().length-1; buffer2 = Number(textEdit.getText()); bodydoc.clear(); result = Math.round((buffer2-32)/1.8); textEdit.setText(buffer1); textEdit.deleteText(textEdit.findText('fub').getEndOffsetInclusive()+1,textEdit.findText('fub').getEndOffsetInclusive()+1+len); textEdit.insertText(textEdit.findText('fub').getEndOffsetInclusive()+1, result); // buffer1 = textEdit.getText(); textEdit.deleteText(0, textEdit.findText('at ').getEndOffsetInclusive()); textEdit.deleteText(textEdit.findText('mph').getStartOffset(), textEdit.getText().length-1); len = textEdit.getText().length-1; buffer2 = Number(textEdit.getText()); bodydoc.clear(); result = Math.round(buffer2*0.44704); textEdit.setText(buffer1); textEdit.deleteText(textEdit.findText('at ').getEndOffsetInclusive()+1,textEdit.findText('at ').getEndOffsetInclusive()+1+len); textEdit.insertText(textEdit.findText('at ').getEndOffsetInclusive()+1, result); textEdit.replaceText('mph', 'm/s'); }; //... textEdit.replaceText('buf', ''); textEdit.replaceText('fub', ''); textEdit.replaceText(degree, "*C"); // textEdit.insertText(textEdit.getText().length, ' '+cityTown); // , if (textEdit.getText().length>70){ textEdit.deleteText(69, textEdit.getText().length-1); } var textSMS = bodydoc.getText(); UrlFetchApp.fetch("http://sms.ru/sms/send?api_id="+apiID+"&to="+phoneNum+"&text="+encodeURI(textSMS)); bodydoc.clear(); }
Source: https://habr.com/ru/post/240533/
All Articles